# Authentication
Source: https://docs.aden.space/docs/developers/sdk/authentication

Pick the right auth mode. API keys for backends, Supabase tokens for user apps.
The SDK supports three ways to authenticate, in order of precedence:

1. `apiKey`: static team API key.
2. `getToken`: async function that returns a token per request.
3. No auth, only public endpoints like `/health` will respond.

All of them send the token as `Authorization: Bearer <token>`.

## Team API keys [#team-api-keys]

Use these for server code, cron jobs and scripts where the credential belongs
to a team, not a person. Never in a browser: Aden refuses a key sent from one
(`403 server_only`).

```ts
const aden = createAdenClient({
    apiKey: process.env.ADEN_API_KEY,
})
```

* Format: `aden_` followed by 64 hex characters.
* Bound to one team. Pass that team as `teamId` on list endpoints; any other
  team is refused.
* **Read only** keys can only read. **Read & write** keys can also change
  milestones, tasks, contacts, webhooks and app data.
* Reaches [these endpoints](/docs/developers/api/authentication#endpoints) only.
* Rate-limited by the team's plan. See [Rate limits](/docs/developers/api/rate-limits).

## Supabase session tokens [#supabase-session-tokens]

Use these for apps where a real person logs in. You re-use the Supabase access
token the user already has, so Row Level Security policies apply.

```ts
import { createClient } from '@supabase/supabase-js'
import { createAdenClient } from '@adenspace/sdk'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

export const aden = createAdenClient({
    getToken: async () => {
        const { data } = await supabase.auth.getSession()
        return data.session?.access_token ?? null
    },
})
```

`getToken` runs on every request, so refreshes and sign-outs are picked up
automatically.

## Custom providers [#custom-providers]

`getToken` is just an async function. Plug it into any auth stack:

```ts
const aden = createAdenClient({
    getToken: async () => {
        const token = await auth0.getAccessTokenSilently({
            audience: 'https://aden.space/api/v1',
        })
        return token ?? null
    },
})
```

Return `null` to make an unauthenticated request.

## What each mode can do [#what-each-mode-can-do]

| Scenario                                   | API key              | Session token      |
| ------------------------------------------ | -------------------- | ------------------ |
| Read tracks, albums, shows, posts          | ✅                    | ✅                  |
| Change milestones, tasks, contacts         | ✅ (Read & write key) | ✅                  |
| Create tracks, upload audio, collaborators | ❌                    | ✅                  |
| Chat, team management, `auth.me`, `user.*` | ❌                    | ✅                  |
| From a browser                             | ❌                    | ✅                  |
| Teams reached                              | Its own team         | The person's teams |
| Rate limit bucket                          | Team plan            | Per person and app |

## Storing keys safely [#storing-keys-safely]

* **Never** put a key in client-side code. Aden refuses a key from a browser, and a
  mobile app leaks it to anyone who unpacks it.
* For apps people sign in to, use [Supabase tokens](#supabase-session-tokens).
* Revoke a key on **Team → API** if you suspect it leaked. It stops working
  within a minute.