# Client credentials

> A service signs in as itself with a key pair (private_key_jwt) or a secret, and gets a short token with the scopes it needs.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/machines/client-credentials/

For agents: OAuth 2.0 client credentials in Sypnia Data: create an OAuth client (cli_…) with token_endpoint_auth_method private_key_jwt (public JWK registered, private key stays with the service) or client_secret_basic/post (syp_cs_… shown once, argon2id at rest). POST /auth/v1/oauth/token grant_type=client_credentials with client_assertion (RFC 7523: iss=sub=client_id, aud=token endpoint, exp ≤ 5 min, unique jti) and optional scope/resource (RFC 8707). Response: access_token (EdDSA JWT, default 15 min for clients), token_type Bearer or DPoP. Client database role default authenticated with sub=client_id; project:admin → service_role.

## Create a client

In the console → Machines → OAuth clients, or with the API:

```bash
curl -X POST https://<ref>.data.sypnia.com/auth/v1/admin/oauth/clients \
  -H "apikey: syp_sk_…" -H "content-type: application/json" \
  -d '{ "name": "billing-worker", "token_endpoint_auth_method": "private_key_jwt",
        "jwks": { "keys": [ { "kty": "OKP", "crv": "Ed25519", "x": "…", "kid": "bw-1" } ] },
        "scopes": ["data:read", "data:write"], "dpop_bound_access_tokens": true }'
```

The answer has the `client_id` (`cli_…`). With a key pair, the **private key never leaves your service**: the engine only knows the public half.

| Authentication                               | When                                                                                                   |
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `private_key_jwt`                            | **Recommended.** Nothing secret is stored by the engine; keys rotate by adding a new public key.       |
| `client_secret_basic` / `client_secret_post` | When a key pair is not possible. The secret (`syp_cs_…`) is shown once and stored as an argon2id hash. |

## Get a token

Sign a short JWT with your private key (RFC 7523) and send it:

```text
POST /auth/v1/oauth/token
content-type: application/x-www-form-urlencoded

grant_type=client_credentials
&client_id=cli_8fK2…
&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
&client_assertion=<JWT: iss=sub=cli_8fK2…, aud=https://<ref>.data.sypnia.com/auth/v1/oauth/token, exp=now+60, jti=random>
&scope=data:read data:write
```

```json
{ "access_token": "eyJ…", "token_type": "DPoP", "expires_in": 900, "scope": "data:read data:write" }
```

- The assertion lives at most 5 minutes and its `jti` works once.
- `scope` must be within the client's allowed scopes; `resource` (RFC 8707) picks the audience when the client may call several.
- Client tokens last 15 minutes by default; ask for a new one when it expires. There is no refresh token for client credentials.

## What the token can do

The client calls the Data API and SQL like anyone else. By default it runs as the `authenticated` role with `sub` set to its `client_id`, so **your row-level security applies to machines too**: write policies with `sypnia.client_id()`. A client with `project:admin` runs as `service_role`.

`@sypnia/data/server` does all of this for you:

```ts
import { createClient } from '@sypnia/data/server';
const sypnia = createClient(url, { client: { id: 'cli_8fK2…', privateKey: process.env.SYPNIA_CLIENT_KEY!, dpop: true } });
```
