# Get an access token

Getting and revoking Twitch access tokens from the Twitch CLI

Source: https://dev.twitch.tv/docs/cli/token-command/

Use the `token` command to get an access token. [Twitch API endpoints](https://dev.twitch.tv/docs/api/reference.md) require an app access token or a user access token (see the endpoint's reference content for token requirements). 

## Prerequisites

Before getting an access token, you must configure the CLI with your application's client ID and secret. For information, see the [Configure](https://dev.twitch.tv/docs/cli/configure-command.md) command.

If you don't configure the CLI before running the `token` command, you're prompted to provide your application's client ID and secret.

## App access token

The following example shows how to get an app access token.

```bash
% twitch token
2021/10/14 09:30:28 App Access Token: 44xwqk6...
```

## User access token

User access tokens require that the user consent to the application accessing their resources. To get consent, you must set the redirect URL to `http://localhost:3000` when [registering your application](https://dev.twitch.tv/docs/authentication/register-app.md).

The following are the flags you use to get a user access token.

| Flag | Shorthand | Description |
|-|-|-|
| \-\-user-token | -u| Tells the `token` command that you want a user access token. |
| \-\-scopes | -s | The space-delimited list of scopes, which identify the resources that the user is giving the app permission to access. To specify multiple scopes, wrap the list in single quotes.|

This example shows how to get a user access token with the **user:read:email** scope set. Note that the call opens a browser to request user consent.

```bash
% twitch token -u -s user:read:email
Opening browser. Press Ctrl+C to cancel...
2021/10/14 09:54:05 User Access Token: 8yl7k3evyn8...
Refresh Token: dt4z0rc33nz2s4z...
Expires At: 2021-10-14 21:16:33.332005 +0000 UTC
Scopes: [user:read:email]
```

This example shows how to specify multiple scopes.

```bash
% twitch token -u -s 'user:read:email user:edit'
```

If you're logged in to Twitch when you request a token, it gets your user information from your log in. Otherwise, you're prompted for your credentials.

## Revoking access tokens

To revoke an access token, use the `--revoke` (`-r`) flag. The flag's parameter identifies the token to revoke.

```bash
% twitch token -r ke1dlej9...                
2021/10/14 10:26:19 Token ke1dlej9... has been successfully revoked.
```

To revoke a token that was created using a different client ID than the one that the CLI is configured with, use the `--client-id` flag.

```bash
twitch token -r nbrw0pvgp... --client-id t214nt8z1...
2021/10/14 10:53:46 Token nbrw0pvgp... has been successfully revoked.
```

## Next steps

Now that you have an access token, you can use the [api](https://dev.twitch.tv/docs/cli/api-command.md) command to send requests to API endpoints.
