> ## Documentation Index
> Fetch the complete documentation index at: https://docs.amika.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Supply a static API key or a dynamically refreshed token to the SDK.

`AmikaClient` needs a credential for every request. Provide exactly one of
three options:

* `apiKey`: a static Amika API key. This is the usual choice.
* `accessToken`: a static bearer token.
* `tokenSource`: an object the client calls each time it needs a token.

## Static key

The simplest case: pass a key string you already have.

```ts theme={null}
import { AmikaClient } from "@amika/sdk";

const client = new AmikaClient({ apiKey: process.env.AMIKA_API_KEY! });
```

Static credentials must be non-empty strings, or the constructor throws.

## The `TokenSource` interface

For tokens that need to be fetched or refreshed, implement `TokenSource`:

```ts theme={null}
interface TokenSource {
  token(): string | Promise<string>;
}
```

The client calls `token()` for every request. Returning a promise lets you
fetch or refresh on demand.

## A refreshing token source

The client awaits `token()` on each request, so you can cache and refresh
inside it.

```ts theme={null}
import { AmikaClient } from "@amika/sdk";
import type { TokenSource } from "@amika/sdk";

class RefreshingTokenSource implements TokenSource {
  private cached?: { token: string; expiresAt: number };

  async token(): Promise<string> {
    if (!this.cached || Date.now() >= this.cached.expiresAt - 60_000) {
      const fresh = await fetchTokenFromYourAuthService();
      this.cached = { token: fresh.token, expiresAt: fresh.expiresAtMs };
    }
    return this.cached.token;
  }
}

const client = new AmikaClient({ tokenSource: new RefreshingTokenSource() });
```

`fetchTokenFromYourAuthService` stands in for your own credential-loading
code.

> [!NOTE]
>
> The SDK never reads `AMIKA_API_KEY` from the environment or disk on your
> behalf. Sourcing the credential, from an env var, a secret manager, or an
> auth service, is always the caller's responsibility.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.