# API Tokens and Sessions

> Create and manage LearnHouse API tokens, choose their permissions, and sign in with user sessions, refresh cookies and two-factor codes.

The LearnHouse API accepts two kinds of credentials:

- **User sessions** (JWT), for browsers and for scripts acting as a specific user.
- **API tokens** (prefixed `lh_`), for server-to-server integrations scoped to one organization.

## User sessions (JWT)

The login endpoint takes form data with `username` (the user's email) and `password`:

```bash
curl -X POST http://localhost:1338/api/v1/auth/login \
  -F 'username=you@example.com' \
  -F 'password=your-password'
```

It returns an access token valid for 8 hours. Send it in the `Authorization` header:

```bash
curl http://localhost:1338/api/v1/users/profile \
  -H "Authorization: Bearer <access_token>"
```

Browsers get the same tokens as `HttpOnly` cookies: `LH_access` (8 hours) and `LH_refresh` (30 days of inactivity; self-hosted instances can change this with `LEARNHOUSE_AUTH_REFRESH_TOKEN_DAYS`, minimum 14). `GET /api/v1/auth/refresh` issues a new access token from the refresh cookie.

If the account has two-factor authentication, `/auth/login` answers `{ "mfa_required": true, "mfa_token": "..." }` instead. Send that `mfa_token` with a 6-digit `code` from the authenticator app (or a backup code with `is_backup_code: true`) to `POST /api/v1/auth/login/mfa` within 5 minutes to get the session.

> Logins are rate-limited by IP, and repeated failed attempts can temporarily lock the account. On LearnHouse Cloud the email address must be verified before login succeeds. See [Authentication](https://docs.learnhouse.app/platform/users/authentication#protection-against-abuse).

### Auth endpoints

| Method | Endpoint | Description |
| --- | --- | --- |
| `POST` | `/api/v1/auth/login` | Sign in with form-encoded `username` and `password` |
| `POST` | `/api/v1/auth/login/mfa` | Complete a sign-in that needs a two-factor code |
| `GET` | `/api/v1/auth/refresh` | Get a new access token |
| `DELETE` | `/api/v1/auth/logout` | Revoke the session |
| `POST` | `/api/v1/auth/oauth` | Sign in with Google |
| `POST` | `/api/v1/auth/magic-link/request` | Email a magic sign-in link |
| `POST` | `/api/v1/auth/magic-link/verify` | Exchange a magic link token for a session |
| `POST` | `/api/v1/auth/verify-email` | Verify an email address |
| `POST` | `/api/v1/auth/resend-verification` | Resend the verification email |

The two-factor management endpoints (`/api/v1/auth/mfa/...`) are listed in the [API Reference](https://docs.learnhouse.app/reference/auth).
In TypeScript, each of these has an SDK method (`lh.auth.login`, `lh.auth.verifyMagicLink`, `lh.auth.mfa.setup`…); see
[Accounts and sign-in](https://docs.learnhouse.app/developers/sdk/accounts).

### Embed sessions

A server holding an API token can mint a short-lived session for one of the org's learners, for the embed components
(`@learnhouse/react`, `@learnhouse/vue`) to act as that learner in a browser:

```bash
curl -X POST https://learn.example.com/api/v1/admin/your-school/auth/embed-session \
  -H "Authorization: Bearer lh_…" -H "Content-Type: application/json" \
  -d '{ "user_id": 42, "ttl_seconds": 900 }'
```

It returns `access_token`, `expires_in`, `expires_at` (Unix seconds), `user_id`, `user_uuid` and `org_id`. The token
needs the **Users: update** permission. Sessions last 15 minutes by default (60 to 3600 seconds), have no refresh
token, work only inside the org and only on the routes the embed components use (other routes answer `403`), and
can't change the account. Admins, maintainers and accounts in other organizations are refused. Like LTI launches,
they are exempt from the org's sign-in method and two-factor rules inside that org, since the server minting them
vouches for the learner. See
[Signed-in learners](https://docs.learnhouse.app/developers/react/learners).

Like every Admin API route (`/api/v1/admin/{org_slug}/...`), it only takes an API token: a request without
credentials gets `401`, and a user session gets `403`. The SDK wraps the whole Admin API as
[`lh.admin`](https://docs.learnhouse.app/developers/sdk/admin).

## API tokens

> API tokens need the **Pro** plan or higher on LearnHouse Cloud. They are available on every self-hosted instance.

API tokens let your own servers call LearnHouse without a user session. Each token belongs to one organization and carries its own permissions.

```bash
curl http://localhost:1338/api/v1/courses/... \
  -H "Authorization: Bearer lh_your_api_token_here"
```

### Creating a token

1. In the dashboard go to **Developers** → **API Access** and click **Create Token**.
2. Fill in the **Token Name** (required, up to 100 characters, unique among the organization's active tokens) and an optional **Description**.
3. Optionally set an **Expiration Date**. Leave it empty for a token that never expires.
4. Under **Permissions**, choose **Read Only**, **Full Access** or **Custom**.
5. Click **Create Token** and copy the token. **This is the only time you'll see it.** LearnHouse only keeps a hash and the first 12 characters, shown as the **Token Prefix**.

Managing tokens needs a role that can manage roles (the Admin role by default) and a signed-in session; a token cannot create or manage other tokens.

### Permissions

With **Custom**, you choose **Create**, **Read**, **Update** and **Delete** for each resource:

| Resource | Covers |
| --- | --- |
| **Courses** | Courses, and course results in search |
| **Activities** | Lessons |
| **Assignments** | Assignments, tasks, submissions and grades. See [Headless Assignments](https://docs.learnhouse.app/developers/api/headless-assignments). |
| **Chapters** | Course chapters |
| **Folders** | Library folders |
| **Media** | Uploaded files |
| **Certifications** | Certificates |
| **User Groups** | User groups and their members |
| **Payments** | Payment data |
| **Users (Admin API)** | Users, through the headless Admin API |
| **Roles (Admin API)** | Roles, through the headless Admin API |
| **Search** | Read only. Lets the token call [search](https://docs.learnhouse.app/platform/search). |

A token can never do more than the user who created it. Its rights are capped by its creator's current role on every request.

### Managing tokens

The **API Access** tab lists each token with its **Token Prefix**, **Status** (Active or Revoked), **Last Used** and **Expires** dates.

- **Regenerate** issues a new secret for the same token. The old value stops working immediately.
- **Revoke** disables the token immediately. This cannot be undone, and a revoked token can no longer be edited or regenerated.

Through the API, `PUT /api/v1/orgs/{org_id}/api-tokens/{token_uuid}` changes a token's name, description, rights or expiry. Omitted fields stay as they are; send `"expires_at": null` to remove the expiry. Names follow the same rules as at creation.

Creating and regenerating tokens is limited to 10 per hour per IP address.

### When a token stops working

- Its expiration date has passed.
- It was revoked.
- The organization dropped below the Pro plan (LearnHouse Cloud).
- Its creator is no longer a member of the organization.

### What tokens cannot do

Tokens only work on the content and admin endpoints listed above, plus the [Zapier endpoints](https://docs.learnhouse.app/platform/automations/zapier) and the [MCP server](https://docs.learnhouse.app/platform/ai/connect-ai-apps) for AI apps. They are refused by organization settings, user accounts, roles, API token management, webhooks, analytics, AI, boards, playgrounds, audit logs, SCORM and code execution endpoints. Use a user session for those.
