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

# Authentication

> Authenticate with the Royaltyport SDK using API tokens or OAuth access tokens.

The SDK authenticates by passing a token to the `apiKey` configuration option. Both API tokens and OAuth access tokens are supported.

## API Token

API tokens use the `rp_` prefix and are created in **Organizations > Settings > Tokens**. Pass the token directly when creating the client:

```js theme={null}
import { Royaltyport } from '@royaltyport/sdk';

const royaltyport = new Royaltyport({
  apiKey: 'rp_your_token_here',
});
```

See [API Token management](/api-reference/authentication) for details on creating and scoping tokens.

***

## OAuth Access Token

OAuth access tokens obtained through the browser-based consent flow can also be used:

```js theme={null}
const royaltyport = new Royaltyport({
  apiKey: oauthAccessToken,
});
```

***

## Environment Variables

The SDK does not automatically read environment variables. To use an environment variable, read it explicitly:

```js theme={null}
const royaltyport = new Royaltyport({
  apiKey: process.env.ROYALTYPORT_TOKEN,
});
```

| Variable              | Description                                                  |
| --------------------- | ------------------------------------------------------------ |
| `ROYALTYPORT_TOKEN`   | API token or OAuth access token                              |
| `ROYALTYPORT_API_URL` | Custom API base URL (default: `https://api.royaltyport.com`) |

```js theme={null}
const royaltyport = new Royaltyport({
  apiKey: process.env.ROYALTYPORT_TOKEN,
  baseUrl: process.env.ROYALTYPORT_API_URL,
});
```

***

## Custom Base URL

Override the default API base URL for staging or self-hosted environments:

```js theme={null}
const royaltyport = new Royaltyport({
  apiKey: 'rp_your_token_here',
  baseUrl: 'https://your-api-url.com',
});
```

***

## Custom Fetch

Pass a custom `fetch` implementation for testing, proxying, or environments without a global `fetch`:

```js theme={null}
import { Royaltyport } from '@royaltyport/sdk';

const royaltyport = new Royaltyport({
  apiKey: 'rp_your_token_here',
  fetch: customFetchImplementation,
});
```
