> For the complete documentation index, see [llms.txt](https://docs.lingoql.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.lingoql.com/sub0/apis-abi/authentication.md).

# Authentication

Authentication in Sub0 has two parts:

1. issue a token with `tokenize`
2. protect later endpoints with `protected`

### Issue a token

Use `tokenize` on sign-up or sign-in style endpoints.

```json
"tokenize": {
  "type": "JWT",
  "algorithm": "HS256",
  "expiration": 3600,
  "encryption_key": "$ENV.JWT_KEY",
  "custom_claim_fields": ["id", "email"],
  "property_name": "token"
}
```

Supported token types:

* `JWT`
* `PASETO`

For `PASETO`, the encryption key must be 32 characters long.

### Protect an endpoint

Use `protected` when a request must include a valid token.

```json
"protected": {
  "provide_as": "Authorization",
  "extract_claims": ["id"]
}
```

Protected endpoints should also define `tokenize` so Sub0 knows how to verify the token.

Default header format:

```json
"Authorization": "Bearer <token>"
```

Custom header example:

```json
"provide_as": "x-api-key"
```

### Use extracted claims

Claims from a verified token are available through `$PROTECTED`.

```json
"parameters": ["$PROTECTED.id"]
```

```json
"upload_folder": "/gallery/$PROTECTED.id"
```

This keeps access control on the server side.

### What Sub0 verifies

Sub0 validates:

* token signature
* token expiration
* token structure

Invalid or missing tokens return `401 Unauthorized`.

### WebSockets

WebSocket connections can include a `uid` for targeted delivery.

Protected WebSocket resources still require a valid token when the resource is marked `protected`.

Use the connection pattern documented in [Websockets](/sub0/apis-abi/websockets.md).

### Best practices

* Keep secrets in `$ENV`
* Extract only the claims you need
* Protect every state-changing endpoint
* Trust `$PROTECTED.id`, not a user ID from the payload

### Next step

Pair authentication with [Making API Requests](/sub0/apis-abi/making-api-requests.md) to see how clients send protected requests.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lingoql.com/sub0/apis-abi/authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
