# Authentication

> Authenticate LemuTickets API requests with a scoped API key or an OAuth app

URL: https://docs.lemuai.com/tickets-api/authentication

There are two ways to call the LemuTickets API:

| Use | When |
|---|---|
| **API key** | Your own server talks to LemuTickets, for example your ERP syncing tickets. This is the usual choice. |
| **OAuth app** | You build an app that acts for a person who approves it (authorization code), or a server integration that should not use a long-lived key (client credentials). |

Both are sent as a bearer token in the `Authorization` header.

**Warning:** API keys, client secrets and access tokens act on your organization's data. Keep them on your server. Never put them in a browser, a mobile app or a public repository. To show tickets in a browser, use an [embed token](https://docs.lemuai.com/tickets-api/embedding) instead.

## API keys

### Create a key

1. In LemuTickets, open **Settings → API keys and webhooks** and stay on the **API keys** tab.
2. Click **Create key**.
3. Fill in the form:
   - **Name**: where the key is used, for example `ERP sync`.
   - **Mode**: **Live** keys start with `ltk_live_`, **Test** keys with `ltk_test_`. The mode only labels the key; both work on the same organization and data.
   - **Scopes**: pick a preset (**Read only**, **Agent** or **Full access**) or **Custom** to choose scopes one by one. See [scopes](#scopes) below.
   - **Projects**: limit the key to some projects. Empty means every project.
   - **Requests per minute**: the key's [rate limit](https://docs.lemuai.com/tickets-api/conventions#rate-limits). The default is 600.
   - **Allowed IP addresses**: single addresses or CIDR ranges the key may be used from. Empty means any address.
   - **Expires**: never, or on a date you choose.
4. Copy the key. The full key is shown **only once**. Store it straight away in a secrets manager or an environment variable.

You need the `api_keys.manage` permission to see this page (organization owners and admins have it). You cannot give a key a scope you do not have yourself.

To stop a key working, revoke it on the same page. A revoked or expired key is rejected at once.

### Send the key
```bash
curl https://api.tickets.lemuai.com/v1/projects \
  -H "Authorization: Bearer ltk_live_your_api_key"
```

The `X-API-Key: ltk_live_your_api_key` header also works.

### Who the key acts as

Each key acts as its own user, named **API:** followed by the key's name (for example `API: ERP sync`). Tickets the key creates show this user as the reporter, and comments it writes show it as the author.

A key does not see restricted tickets unless they are shared with that user, and it only sees the projects you chose when you created it.

## Scopes

Scopes are the same permissions team members have. A request that needs a scope the key does not have fails with `403` and the code `missing_capability`.

| Scope | Allows |
|---|---|
| `tickets.read` | See tickets in projects the key can access |
| `tickets.write` | Create and edit tickets, comment, transition |
| `tickets.delete` | Delete and merge tickets |
| `tickets.assign` | Assign tickets to others |
| `tickets.bulk` | Bulk update tickets |
| `comments.internal` | Read and write internal notes |
| `requesters.read` | See requesters and accounts |
| `requesters.write` | Create and edit requesters and accounts |
| `pii.read` | See unmasked phone numbers and emails |
| `projects.read` | See projects |
| `projects.admin` | Create projects, edit workflows, types and fields |
| `config.admin` | SLAs, escalations, automations, assignment, calendars, macros |
| `teams.admin` | Manage teams, skills and shifts |
| `integrations.admin` | Connect Slack, email, Lemu, Jira and other integrations |
| `api_keys.manage` | Create and revoke API keys and webhooks |
| `reports.read` | See reports and dashboards |
| `audit.read` | See and export the audit log |
| `security.admin` | Roles, SSO, IP allowlist, retention, sandboxes |
| `data.export` | Export and import data |
| `copilot.use` | Use the Copilot |
| `approvals.decide` | Approve or reject approval requests |
| `assets.read` | See assets |
| `assets.write` | Create and edit assets |
| `field_service.dispatch` | Schedule and dispatch field visits |
| `kb.write` | Write knowledge base articles |

The presets give a key these scopes:

| Preset | Scopes |
|---|---|
| **Read only** | `tickets.read`, `requesters.read`, `projects.read`, `reports.read` |
| **Agent** | `tickets.read`, `tickets.write`, `tickets.assign`, `tickets.bulk`, `comments.internal`, `requesters.read`, `requesters.write`, `pii.read`, `projects.read`, `reports.read`, `copilot.use`, `assets.read`, `assets.write`, `kb.write` |
| **Full access** | Every scope |

Without `pii.read`, emails and phone numbers in responses are partly hidden, for example `p•••@example.com` and `+91•••••••210`.

## OAuth apps

Register an OAuth app when someone other than your own server needs access, or when you want short-lived tokens instead of a key.

### Register an app

1. Open **Settings → API keys and webhooks** and select the **OAuth apps** tab.
2. Click **Register app** and fill in **App name**, an optional **Logo URL** and **Description**, the **Redirect URIs**, and the scopes it can ask for.
3. Leave **Can keep a secret** on for apps with a server. Turn it off for mobile or single-page apps: they get no client secret and must use PKCE.
4. Copy the **client ID** (`ltc_...`) and, for apps that can keep a secret, the **client secret** (`ltcs_...`). The secret is shown only once.

Redirect URIs must match exactly and use `https` (`http` is allowed only for `localhost`).

### Authorization code with PKCE

Use this when your app acts for a LemuTickets user. The token can never do more than that user can do now, and only within the scopes they approved.

**1. Send the user to the consent screen.** Create a random `code_verifier` (43 to 128 characters) and its SHA-256 `code_challenge` (base64url, no padding), then open:
```
https://tickets.lemuai.com/oauth/authorize
  ?response_type=code
  &client_id=ltc_your_client_id
  &redirect_uri=https://erp.example.com/oauth/callback
  &scope=tickets.read tickets.write
  &state=af0ifjsldkj
  &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
  &code_challenge_method=S256
```

The URL is split over lines here to make it easier to read; send it as one line with the values URL-encoded. PKCE with `S256` is required for every app. `scope` is a space-separated list; leave it out to ask for all of the app's scopes. After the user approves, they return to your `redirect_uri` with `code` and your `state`. The code expires after 5 minutes and works once.

**2. Exchange the code for tokens:**
```bash
curl -X POST https://api.tickets.lemuai.com/v1/oauth/token \
  -u "ltc_your_client_id:ltcs_your_client_secret" \
  -d grant_type=authorization_code \
  -d code=ltac_code_from_the_redirect \
  -d redirect_uri=https://erp.example.com/oauth/callback \
  -d code_verifier=your_code_verifier
```

Apps without a secret send `client_id` in the body instead of `-u`, and no secret.
```json
{
  "access_token": "lto_9kq2...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "tickets.read tickets.write",
  "refresh_token": "ltr_X4b7..."
}
```

**3. Call the API** with `Authorization: Bearer lto_...`. Access tokens last 1 hour.

**4. Refresh** before the access token expires. Refresh tokens last 30 days and change every time you use one: store the new one each time. Using an old refresh token again ends the whole grant, and the user has to approve the app again.
```bash
curl -X POST https://api.tickets.lemuai.com/v1/oauth/token \
  -u "ltc_your_client_id:ltcs_your_client_secret" \
  -d grant_type=refresh_token \
  -d refresh_token=ltr_X4b7...
```

You can send a smaller `scope` when refreshing, but not a wider one.

### Client credentials

Use this for a server integration that acts for your organization, not for a person. It needs an app that can keep a secret.
```bash
curl -X POST https://api.tickets.lemuai.com/v1/oauth/token \
  -u "ltc_your_client_id:ltcs_your_client_secret" \
  -d grant_type=client_credentials \
  -d "scope=tickets.read tickets.write"
```

The response has an `access_token` that lasts 1 hour and no refresh token: ask for a new one when it expires. Leave out `scope` to get all of the app's scopes.

A client credentials token is not tied to a user, so endpoints that record a person need one in the request. For example, [Log work](https://docs.lemuai.com/tickets-api/tickets/log-work) needs `user_id`.

### Token endpoint details

| | |
|---|---|
| Token URL | `POST https://api.tickets.lemuai.com/v1/oauth/token` |
| Revoke URL | `POST https://api.tickets.lemuai.com/v1/oauth/revoke` with `token=...` |
| Client authentication | HTTP Basic (`client_secret_basic`), or `client_id` and `client_secret` in the body (`client_secret_post`) |
| Body format | `application/x-www-form-urlencoded` or JSON |
| Server metadata | `https://api.tickets.lemuai.com/.well-known/oauth-authorization-server` |

Token endpoint errors use the OAuth format, not the API's usual error shape:
```json
{
  "error": "invalid_grant",
  "error_description": "The code expired"
}
```

| `error` | Meaning |
|---|---|
| `invalid_client` | Unknown client, wrong secret, or a secret sent for an app that has none (`401`) |
| `invalid_grant` | The code or refresh token is unknown, used, expired or revoked, or the PKCE check or `redirect_uri` does not match |
| `invalid_scope` | The app may not ask for these scopes |
| `unauthorized_client` | Client credentials asked for by an app without a secret |
| `unsupported_grant_type` | `grant_type` is not `authorization_code`, `refresh_token` or `client_credentials` |

Removing an app in **Settings → API keys and webhooks** revokes all of its tokens.

## Authentication errors

| Status | Code | Meaning |
|---|---|---|
| `401` | `unauthenticated` | No key or token was sent |
| `401` | `invalid_api_key` | The key is wrong, revoked or expired |
| `401` | `invalid_token` | The OAuth access token is wrong, revoked or expired, or the app was removed |
| `403` | `ip_not_allowed` | The request came from an IP address the key, or your organization, does not allow |
| `403` | `missing_capability` | The key or token does not have the scope this endpoint needs |
```json
{
  "error": {
    "type": "unauthorized",
    "code": "invalid_api_key",
    "message": "Invalid API key",
    "request_id": "4f1d9c2b7a6e4e0f8c3b1a2d5e6f7a8b"
  }
}
```
