# Get an OAuth 2.0 token

`POST /v1/oauth2/token`

Endpoints: `https://{tenant}.core.server.test.neobits.ai/v1/oauth2/token`, `https://{tenant}.core.server.prod.neobits.ai/v1/oauth2/token`
OpenAPI document: https://docs.neobits.ai/openapi/core-authz.json

Authentication: `basic` (HTTP Basic authentication with your client's username and password: `Authorization: Basic base64(username:password)`.)

Issues an access token with the client credentials grant. Authenticate with HTTP Basic, using your client's `username` and `password`: `Authorization: Basic base64(username:password)`. Use the password exactly as you received it when the client was created.

## Request body

Required: yes

Content type: `application/x-www-form-urlencoded`

Schema (JSON Schema):

```json
{"type":"object","title":"TokenRequest","required":["grant_type","resource"],"example":{"grant_type":"client_credentials","resource":"https://your-tenant.fuse.server.test.neobits.ai","scope":"inquiry:read inquiry:write person:read","session_duration":3600},"properties":{"grant_type":{"type":"string","description":"OAuth 2.0 grant type","enum":["client_credentials"]},"resource":{"type":["string","array"],"items":{"type":"string"},"example":"https://your-tenant.fuse.server.test.neobits.ai","description":"The API host the token is for, which becomes its audience, such as `https://your-tenant.fuse.server.test.neobits.ai`. Send it several times for several APIs. Each must be your tenant's host of an API your client has a scope for; any other resource is rejected with 403."},"scope":{"type":"string","description":"Space-separated list of scopes for the token (cannot be combined with role). Each scope must be assigned to the client; otherwise the request fails with `invalid_scope`. The token carries only the scopes requested here, so request every scope the target endpoints need.","example":"inquiry:read inquiry:write person:read"},"role":{"type":"string","description":"Role for the token (cannot be combined with scope). The token carries every scope of the role. Only a client with a role can request one, and only its own role or a less privileged one; otherwise the request fails with 403.","enum":["owner","admin","member","viewer","guest"]},"session_duration":{"type":"integer","minimum":0,"maximum":28800,"description":"Requested token lifetime in seconds. The issued lifetime is the lower of this value and the client's `max_session_duration`. When omitted, the token lives for the client's `max_session_duration`.","example":3600}}}
```

Example:

```json
{
  "grant_type": "client_credentials",
  "resource": "https://your-tenant.fuse.server.test.neobits.ai",
  "scope": "inquiry:read inquiry:write person:read",
  "session_duration": 3600
}
```

## Example request

```bash
curl --request POST \
  --url https://your-tenant.core.server.test.neobits.ai/v1/oauth2/token \
  --header 'Authorization: Basic REPLACE_BASIC_AUTH' \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data grant_type=client_credentials \
  --data resource=https%3A%2F%2Fyour-tenant.fuse.server.test.neobits.ai \
  --data 'scope=inquiry%3Aread+inquiry%3Awrite+person%3Aread' \
  --data session_duration=3600
```

## Responses

### 200 Access token response

Content type: `application/json`

Schema (JSON Schema):

```json
{"oneOf":[{"type":"object","title":"ScopeBasedToken","properties":{"access_token":{"type":"string","description":"The issued JWT access token"},"token_type":{"type":"string","description":"Type of token","enum":["Bearer"]},"expires_in":{"type":"integer","description":"Token lifetime in seconds. At most the client's `max_session_duration` (28800 seconds by default).","example":3600},"scope":{"type":"string","description":"Space-separated list of scopes associated with the token"}}},{"type":"object","title":"RoleBasedToken","properties":{"access_token":{"type":"string","description":"The issued JWT access token"},"token_type":{"type":"string","description":"Type of token","enum":["Bearer"]},"expires_in":{"type":"integer","description":"Token lifetime in seconds. At most the client's `max_session_duration` (28800 seconds by default).","example":3600},"role":{"type":"string","description":"Role associated with the token"}}}]}
```

Example:

```json
{
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "inquiry:read inquiry:write person:read"
}
```

## Error responses

Error bodies follow the format in the [error reference](https://docs.neobits.ai/api-reference/errors.md), which lists every error code.

| Status | Meaning | Example body |
| --- | --- | --- |
| 400 | OAuth 2.0 Bad Request. - `invalid_scope` with `400-116`: your client isn't assigned a scope in `scope`. `error_description` names each one, such as `The client is not assigned the scope 'terms:read'.` Request only the scopes your client holds, or ask whoever created the client to assign the missing ones. - `invalid_grant` with `400-110`, `400-111`, `400-112` or `400-115`: the authorization code or refresh token sent with the `authorization_code` or `refresh_token` grant is invalid, expired, already used, revoked or issued to another client, or doesn't cover the requested `resource`. - `invalid_request`: a parameter is missing, unknown or invalid, such as a `scope` that doesn't exist or `scope` sent together with `role`. `error_code` says which, for example `400-001` for a missing parameter and `400-003` for an invalid value. | `{"error":"invalid_scope","error_code":"400-116","error_uri":"https://docs.neobits.ai/api-reference/errors","error_description":"The client is not assigned the scope 'terms:read'.","error_timestamp":"2026-11-08T10:15:32.104Z"}` |
| 401 | OAuth 2.0 Unauthorized. Your client couldn't authenticate. The `WWW-Authenticate` header names HTTP Basic, the scheme this endpoint accepts. - `invalid_client` with `401-001`: the `username` or `password` is wrong, or the `Authorization` header doesn't hold valid Basic credentials. - `invalid_client` with `401-006`: the client is disabled. - `unauthorized` with `401-000`: the request has no `Authorization` header, or the client has neither scopes nor a role. | `{"error":"invalid_client","error_code":"401-001","error_uri":"https://docs.neobits.ai/api-reference/errors","error_description":"Client is not authenticated.","error_timestamp":"2026-11-08T10:15:32.104Z"}` |
| 403 | OAuth 2.0 Forbidden. The client authenticated but may not get this token: a requested `resource` isn't allowed for it, the requested `role` is more privileged than its own, or the client has no role. | `{"error":"access_denied","error_code":"403-001","error_uri":"https://docs.neobits.ai/api-reference/errors","error_description":"Resource 'https://your-tenant.fuse.server.test.neobits.ai' is not allowed for this client.","error_timestamp":"2025-06-25T15:29:49.992Z"}` |
| 404 | OAuth 2.0 Not Found | `{"error":"not_found","error_code":"404-000","error_uri":"https://docs.neobits.ai/api-reference/errors","error_description":"The requested resource was not found.","error_timestamp":"2025-06-25T15:29:49.992Z"}` |
| 429 | OAuth 2.0 Too Many Requests. When the rate limit rejects a request, the response carries `Retry-After` and no `error_timestamp` or `X-Request-ID`, as the request never reached the API. | `{"error":"too_many_requests","error_code":"429-000","error_uri":"https://docs.neobits.ai/api-reference/errors","error_description":"Too many requests. Please try again later."}` |
| 500 | OAuth 2.0 Internal Server Error | `{"error":"server_error","error_code":"500-000","error_uri":"https://docs.neobits.ai/api-reference/errors","error_description":"The authorization server encountered an unexpected condition.","error_timestamp":"2025-06-25T15:29:49.992Z"}` |
| 504 | OAuth 2.0 Gateway Timeout | `{"error":"gateway_timeout","error_code":"504-000","error_uri":"https://docs.neobits.ai/api-reference/errors","error_description":"Timeout of 29s reached. Closing connection.","error_timestamp":"2025-06-25T15:29:49.992Z"}` |