Set up your institution, step by step. Start here.

How to Use the Course37 JavaScript SDK

Show your catalog and website pages, and sign students in, in any JavaScript site or app with @course37/sdk — a small typed client that refreshes sessions for you.


@course37/sdk is the JavaScript and TypeScript client for the Course37 Developer API. Use it to show your catalog and website pages, and to sign students in and teach them, in any JavaScript website or app. The starter website is built on it.

  • No dependencies. It works in browsers, Node.js 18+, Bun, Deno and edge runtimes — anywhere with fetch.
  • Typed replies, and one error type, Course37Error, for every failure.
  • Student sessions are handled for you: tokens refresh automatically, browser tabs share one session, and sign-outs from other devices are noticed.

It is open source (MIT licence): github.com/Shaitrish/course37-js.


Install

npm install @course37/sdk

You need a key from Integrations → Developer API — see Set Up the Developer API.

Key Where it can be used
Publishable, pk_live_… Browser and server. Add every website that uses it to Allowed websites
Secret, sk_live_… Server only — the API refuses it from a browser

Read Your Catalog and Pages

This works on a server or in a browser, in Hybrid and Headless mode:

import { createCourse37Client } from '@course37/sdk';

const course37 = createCourse37Client({
  apiKey: process.env.COURSE37_KEY!, // pk_… or sk_…
  session: false,                    // no student session on the server
});

const org = await course37.org.get(); // name, logo, theme, studentPortalUrl
const { data: courses } = await course37.courses.list();
const course = await course37.courses.get('hsc-physics');
const home = await course37.pages.home(); // your home page's sections

org.studentPortalUrl is your Course37 site. Link to it for anything your website doesn't do itself, such as checkout.


Sign Students In

Student sign-in needs Headless mode.

Recommended: proxy mode. A small route on your own server keeps the 30-day refresh token in a cookie that page scripts can't read. The browser only ever holds the 15-minute access token.

// app/api/course37/[action]/route.ts
// Next.js here; any framework with Request/Response works.
import { createSessionProxy } from '@course37/sdk/proxy';

const handler = createSessionProxy({
  apiKey: process.env.COURSE37_KEY!,
  cookiePath: '/api/course37', // the path this route is mounted on
});
export const POST = (request: Request) => handler(request);
// in the browser
import { createCourse37Client } from '@course37/sdk';

const course37 = createCourse37Client({
  apiKey: 'pk_live_…',
  session: { mode: 'proxy', endpoint: '/api/course37' },
  onSignedOut: (reason) => console.log('signed out:', reason),
});

await course37.session.restore(); // signed in on an earlier visit?
await course37.auth.signIn({ identifier: 'student@example.com', password: '…' });

const me = await course37.me.get();
const { data: enrollments } = await course37.me.enrollments();

Direct mode keeps the tokens in localStorage and talks to the API only. Use it where you have no server of your own, such as a static site or a Capacitor or Electron app. Pass session: { mode: 'direct' }; it's the default in a browser.

To follow the session, subscribe to it:

const unsubscribe = course37.session.subscribe(() => {
  const s = course37.session.getSnapshot();
  // s.status: 'unknown' | 'signed_in' | 'signed_out'
});

In React, use useSyncExternalStore(course37.session.subscribe, course37.session.getSnapshot).


Every Call

Call API route
org.get() GET /v1/org
courses.list(), courses.get(slug) GET /v1/courses, GET /v1/courses/{slug}
pages.get(slug), pages.home() GET /v1/pages/{slug}
auth.sendSignupOtp(identifier) POST /v1/auth/signup/send-otp
auth.signUp({ identifier, otp, password, name }) POST /v1/auth/signup
auth.signIn({ identifier, password }) POST /v1/auth/signin
auth.signOut() POST /v1/auth/signout
auth.forgotPassword(identifier), auth.verifyResetOtp(…), auth.resetPassword(…) POST /v1/auth/password/…
me.get() GET /v1/me
me.sessions.list(), me.sessions.revoke(id) GET /v1/me/sessions, DELETE /v1/me/sessions/{id}
me.enrollments() GET /v1/me/enrollments
me.module(courseSlug, moduleId) GET /v1/me/courses/{slug}/modules/{moduleId}
me.curriculum(courseSlug) GET /v1/me/courses/{slug}/curriculum
me.lesson(id), me.completeLesson(id) GET /v1/me/lessons/{id}, POST /v1/me/lessons/{id}/complete
me.videoProgress(id), me.saveVideoProgress(id, …) GET and POST /v1/me/videos/{id}/progress
me.paymentSummary() GET /v1/me/payments/summary

Every call takes an optional last argument, { signal }, to cancel it.


Errors

Every failure throws a Course37Error:

import { isCourse37Error } from '@course37/sdk';

try {
  await course37.auth.signIn({ identifier, password });
} catch (error) {
  if (isCourse37Error(error)) {
    error.status;     // 401
    error.code;       // 'invalid_credentials'
    error.message;    // safe to show to the student
    error.retryAfter; // seconds, on 429
  }
}

error.code is the API's own code — the Developer API Reference lists them all. The SDK adds four of its own: not_enrolled, validation_failed, network_error and timeout.


How Sessions Work

  • The access token is refreshed 30 seconds before it expires. A request that still gets 401 refreshes and retries once.
  • A refresh token works only once. Browser tabs take turns through navigator.locks, so only one tab refreshes and the others pick up the new token.
  • The session ends for good, and onSignedOut fires, on:
    • refresh_token_reused — an old refresh token came back, which may mean it was stolen, so the session was signed out;
    • refresh_token_expired — after 30 days;
    • session_ended — signed out from another device, or by a password reset.

Related: Developer API Reference · Build Your Own Student Website · Set Up the Developer API

#sdk #javascript #typescript #@course37/sdk #developer api #student sign-in #react #next.js