# Authenticate with an API token

Use an [API token](https://www.datocms.com/docs/content-management-api/authentication.md) to authenticate the CLI when OAuth isn't an option, typically in CI/CD pipelines where there is no browser. The CLI resolves authentication in this order:

1.  `--api-token` flag on the command
    
2.  linked project via OAuth (when `datocms.config.json` is present and you are logged in)
    
3.  environment variable (`DATOCMS_API_TOKEN` for the default profile).
    

The API token must have access to the Content Management API (`can_access_cma: true`). The exact set of role permissions you need depends on what you do with the CLI: schema migrations need permissions on models/fields, environment commands need environment management, maintenance mode needs the maintenance toggle, and so on.

You can pass the token as a parameter to every command:

Terminal window

```bash
$ npx datocms migrations:run --api-token=<YOUR-API-TOKEN> [...]
```

Or expose it as an environment variable:

Terminal window

```bash
$ export DATOCMS_API_TOKEN=<YOUR-API-TOKEN>
$ npx datocms migrations:run [...]
```

The CLI also loads environment variables from a `.env` or `.env.local` file, so you can place the token there. Make sure not to commit the file to your repo:

Terminal window

```bash
$ echo '.env' >> .gitignore
$ echo 'DATOCMS_API_TOKEN=<YOUR-API-TOKEN>' >> .env
```

## Related content in "DatoCMS CLI"

- [CLI Overview](https://www.datocms.com/docs/cli.md)
- [Environment, migration and maintenance commands](https://www.datocms.com/docs/cli/environment-migration-and-maintenance-commands.md)
- [Generating TypeScript types from your schema](https://www.datocms.com/docs/cli/generating-typescript-schema.md)
- [Importing content from other CMSs](https://www.datocms.com/docs/cli/importing-from-other-cms.md)
- [CLI for AI coding agents](https://www.datocms.com/docs/cli/cli-commands-for-ai-coding-agents.md)
- [Authenticate with an API token](https://www.datocms.com/docs/cli/authenticate-with-api-token.md)
- [Profiles and multi-project setup](https://www.datocms.com/docs/cli/profiles-and-multi-project-setup.md)