نمونه‌های آمادهٔ اتصال

زبان خودتان را انتخاب کنید

هر سه نمونه دقیقاً همان دو درخواست را می‌فرستند. شناسهٔ شخص از نشست معتبر سرور LMS گرفته می‌شود.

Node.js

ابتدا کلاینت را با baseUrl و apiKey بسازید. این ماژول در سرور LMS اجرا می‌شود و وابستگی خارجی ندارد.

دریافت کلاینت کامل

۱. ثبت کلاس

Create classroom
const { class_url } = await client.createClass(classData);
// Save class_url with this class in the LMS.

۲. دریافت لینک ورود و هدایت شخص

Get personal entry link
// After LMS authentication and class membership authorization:
const { entry_url } = await client.getEntryLink(classId, authenticatedUser.externalId);
response.writeHead(303, {
  Location: entry_url,
  'Cache-Control': 'no-store',
  'Referrer-Policy': 'no-referrer',
});
response.end();
نمایش فایل کامل کلاینت Node.js
artanclass.mjs
// Node.js 22+; use this module in the LMS SERVER. No packages required.
export function artanClassClient({ baseUrl, apiKey }) {
  const origin = new URL(baseUrl);
  if (origin.protocol !== 'https:') throw new Error('ArtanClass requires HTTPS');
  async function post(path, body, timeout = 30000) {
    const response = await fetch(new URL(path, origin), {
      method: 'POST', redirect: 'error',
      headers: { Authorization: `Bearer ${apiKey}`, 'Content-Type': 'application/json', Accept: 'application/json' },
      body: JSON.stringify(body), signal: AbortSignal.timeout(timeout),
    });
    const data = await response.json();
    if (!response.ok) {
      const error = new Error((data.errors || ['ArtanClass request failed']).join(' '));
      error.status = response.status; error.code = data.code;
      error.retryAfter = response.headers.get('Retry-After');
      throw error;
    }
    return data;
  }
  return {
    createClass: (classData) => post('/api/v1/lms/classes', classData, 180000),
    getEntryLink: (classId, externalUserId) => post(`/api/v1/lms/classes/${encodeURIComponent(classId)}/launch`, {
      external_user_id: externalUserId,
    }),
  };
}

// 1. Create a classroom with its teacher and FULL student list:
// import { readFile } from 'node:fs/promises';
// const client = artanClassClient({
//   baseUrl: process.env.ARTANCLASS_BASE_URL,
//   apiKey: process.env.ARTANCLASS_API_KEY,
// });
// const classData = JSON.parse(await readFile('./class.json', 'utf8'));
// const { class_url } = await client.createClass(classData);
// Store class_url with the LMS class.
//
// 2. In the LMS entry handler, FIRST authenticate and check class membership.
// Take these IDs from YOUR database/session, never directly from a browser form.
// const { entry_url } = await client.getEntryLink(lmsClass.externalId, authenticatedUser.externalId);
// response.writeHead(303, {
//   Location: entry_url,
//   'Cache-Control': 'no-store', 'Referrer-Policy': 'no-referrer',
// });
// response.end();
// Same code for teachers and students. Role is determined by the saved roster.
// Handle API errors before redirecting; do not log keys, national IDs or entry_url.
ورود دانش‌آموز هم همین کد است.

شناسهٔ شخص را از سرور LMS بردارید. معلم و دانش‌آموز پارامتر متفاوتی ندارند؛ خطای API را پیش از هدایت مرورگر مدیریت کنید.