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

# Setup with Client Credentials Grant

> Create a Server app with Client Credentials Grant, configure its access level and scopes, and request an access token.

## Prerequisites

To set up a Platform App using server-side authentication, you need to ensure
you have access to the [Developer Console][devconsole] from your Box enterprise
account. Alternatively, you may sign up for a [developer account][devaccount].

Your Box account also needs [multi-factor authentication (MFA)](https://support.box.com/hc/en-us/articles/360043697154-Multi-Factor-Authentication-Set-Up-for-Your-Account)
enabled, because Box requires it before it shows the application's client secret
on the **Configuration** tab.

## App creation steps

### Create a Server Authentication app

1. Log into Box and go to the [Developer Console][devconsole].
2. Select **New App**.
3. Select **Server** as the app type.
4. Leave **Client Credentials Grant** selected under **Select Method**.
5. Select **Create**.

Client Credentials Grant (CCG) is preselected for new Server Authentication
apps, so selecting **Create** without changing the method produces a CCG app.
Your enterprise's **Switch server app auth type (CCG or JWT)** setting only
affects whether you can change the method later. It is enabled by default for
free developer accounts and disabled by default for enterprises, so in an
enterprise treat the method as fixed once the app is created.

> [!NOTE]
>
> When switching is enabled, you can change to
> JWT at any time from the
> **Configuration** tab. See
> Switch between CCG and JWT
> for details.

## App authorization

Server authentication applications must be authorized before use. The authorization process depends on your account type.

**Free developer accounts**: Your app is automatically authorized when you create it. If authorization did not complete, the **Configuration** tab displays a prompt to authorize the app.

**Enterprise accounts**: After you create the app, the **Configuration** tab prompts you to submit the app for admin approval.

**Enterprise admins and co-admins**: The **Configuration** tab lets you authorize the app directly after creation.

> [card] [Learn more about the authorization process](https://developer.box.com/guides/authorization/platform-app-approval.md)

## Basic configuration

### Application access

An application's access level determines which users and content your app can
access. By default, an application can only successfully interact with the
content of its Service Account and any App Users. To also
access existing Managed Users of an enterprise, navigate to the
**App Access Level** setting on the **Configuration** tab of the
[Developer console][devconsole] and set to **App + Enterprise Access**.

![App access level](https://mintcdn.com/box/J0IlfoFf_v0hLHOU/images/guides/authentication/app-access-level-new.png?fit=max\&auto=format\&n=J0IlfoFf_v0hLHOU\&q=85\&s=8e1e5f2e6b2d8b5fb45f50d0798c5064)

> [!NOTE]
>
> To authenticate as a Managed User or Admin, enable **Generate User Access Tokens** in the **Additional Configuration** section of the **Configuration** tab.

### Application scopes

An application's scopes determine which endpoints and resources an application
can successfully call. See the scopes guide for detailed information
on each option.

![App scopes](https://mintcdn.com/box/KBEcg4yicgc_HMRY/images/guides/authentication/app-scopes.png?fit=max\&auto=format\&n=KBEcg4yicgc_HMRY\&q=85\&s=7d6a76fe395fc483d33179f6c4ad34e1)

### CORS domains

If your application makes API calls from front-end browser code in
Javascript, the domain that these calls are made from needs to be
added to an allow-list due to [Cross Origin Resource Sharing][cors],
also known as CORS. If all requests are made from server-side code,
you may skip this section.

To add the full URI(s) to the allow-list, navigate to the **CORS Domain**
section at the bottom of the **Configuration** tab in the
[Developer console][devconsole].

![App CORS config](https://mintcdn.com/box/KBEcg4yicgc_HMRY/images/guides/authentication/app-cors.png?fit=max\&auto=format\&n=KBEcg4yicgc_HMRY\&q=85\&s=12de7f8cec95dc4317b02172ef10e9df)

## Get an access token

Post your client ID and client secret to the
token endpoint with a
`grant_type` of `client_credentials`, and name the subject you want to
authenticate as. The SDKs send that same request when you retrieve a token.

> [tab] Service Account
>
> ```sh cURL theme={null}
> curl -X POST https://api.box.com/oauth2/token \
>     -d "grant_type=client_credentials" \
>     -d "client_id=[BOX_CLIENT_ID]" \
>     -d "client_secret=[BOX_CLIENT_SECRET]" \
>     -d "box_subject_type=enterprise" \
>     -d "box_subject_id=[BOX_ENTERPRISE_ID]"
> ```
>
> ```cs .NET v10 theme={null}
> using Box.Sdk.Gen;
>
> var config = new CcgConfig(
>     clientId: "[BOX_CLIENT_ID]",
>     clientSecret: "[BOX_CLIENT_SECRET]",
>     enterpriseId: "[BOX_ENTERPRISE_ID]"
> );
> var auth = new BoxCcgAuth(config: config);
> await auth.RetrieveTokenAsync();
> ```
>
> ```swift Swift v10 theme={null}
> import BoxSdkGen
>
> let config = CCGConfig(
>     clientId: "[BOX_CLIENT_ID]",
>     clientSecret: "[BOX_CLIENT_SECRET]",
>     enterpriseId: "[BOX_ENTERPRISE_ID]"
> )
> let auth = BoxCCGAuth(config: config)
> try await auth.retrieveToken()
> ```
>
> ```java Java v10 theme={null}
> CCGConfig config = new CCGConfig.Builder("[BOX_CLIENT_ID]", "[BOX_CLIENT_SECRET]")
>     .enterpriseId("[BOX_ENTERPRISE_ID]")
>     .build();
> BoxCCGAuth auth = new BoxCCGAuth(config);
> auth.retrieveToken();
> ```
>
> ```typescript Node/TypeScript v10 theme={null}
> import { BoxClient } from 'box-node-sdk';
> import { BoxCcgAuth, CcgConfig } from 'box-node-sdk/box';
>
> const ccgConfig = new CcgConfig({
>   enterpriseId: 'YOUR_ENTERPRISE_ID',
>   clientId: 'YOUR_CLIENT_ID',
>   clientSecret: 'YOUR_CLIENT_SECRET',
> });
> const ccgAuth = new BoxCcgAuth({ config: ccgConfig });
> const client = new BoxClient({ auth: ccgAuth });
> ```
>
> ```python Python v10 theme={null}
> from box_sdk_gen import BoxCCGAuth, CCGConfig
>
> config = CCGConfig(
>     client_id="[BOX_CLIENT_ID]",
>     client_secret="[BOX_CLIENT_SECRET]",
>     enterprise_id="[BOX_ENTERPRISE_ID]",
> )
> auth = BoxCCGAuth(config=config)
> auth.retrieve_token()
> ```

> [tab] App User
>
> ```sh cURL theme={null}
> curl -X POST https://api.box.com/oauth2/token \
>     -d "grant_type=client_credentials" \
>     -d "client_id=[BOX_CLIENT_ID]" \
>     -d "client_secret=[BOX_CLIENT_SECRET]" \
>     -d "box_subject_type=user" \
>     -d "box_subject_id=[BOX_USER_ID]"
> ```
>
> ```cs .NET v10 theme={null}
> using Box.Sdk.Gen;
>
> var config = new CcgConfig(
>     clientId: "[BOX_CLIENT_ID]",
>     clientSecret: "[BOX_CLIENT_SECRET]",
>     userId: "[BOX_USER_ID]"
> );
> var auth = new BoxCcgAuth(config: config);
> await auth.RetrieveTokenAsync();
> ```
>
> ```swift Swift v10 theme={null}
> import BoxSdkGen
>
> let config = CCGConfig(
>     clientId: "[BOX_CLIENT_ID]",
>     clientSecret: "[BOX_CLIENT_SECRET]",
>     userId: "[BOX_USER_ID]"
> )
> let auth = BoxCCGAuth(config: config)
> try await auth.retrieveToken()
> ```
>
> ```java Java v10 theme={null}
> CCGConfig config = new CCGConfig.Builder("[BOX_CLIENT_ID]", "[BOX_CLIENT_SECRET]")
>     .userId("[BOX_USER_ID]")
>     .build();
> BoxCCGAuth auth = new BoxCCGAuth(config);
> auth.retrieveToken();
> ```
>
> ```typescript Node/TypeScript v10 theme={null}
> import { BoxClient } from 'box-node-sdk';
> import { BoxCcgAuth, CcgConfig } from 'box-node-sdk/box';
>
> const ccgConfig = new CcgConfig({
>   userId: 'YOUR_USER_ID',
>   clientId: 'YOUR_CLIENT_ID',
>   clientSecret: 'YOUR_CLIENT_SECRET',
> });
> const ccgAuth = new BoxCcgAuth({ config: ccgConfig });
> const client = new BoxClient({ auth: ccgAuth });
> ```
>
> ```python Python v10 theme={null}
> from box_sdk_gen import BoxCCGAuth, CCGConfig
>
> config = CCGConfig(
>     client_id="[BOX_CLIENT_ID]",
>     client_secret="[BOX_CLIENT_SECRET]",
>     user_id="[BOX_USER_ID]",
> )
> auth = BoxCCGAuth(config=config)
> auth.retrieve_token()
> ```

The same user request authenticates an App User, a Managed User, or an Admin.
Which of them you can request a token for depends on the app's configuration: an
App User needs **Generate User Access Tokens**, and a Managed User or an Admin
needs **App + Enterprise Access** as well.

## Common errors

### Grant credentials are invalid

During authentication, you can encounter the following error:

```sh theme={null}
Grant credentials are invalid [400 Bad Request] invalid_grant - Grant credentials are invalid
```

This error indicates one of the following:

* The client ID and client secret passed are incorrect or are not for the same application.

* The `box_subject_id` can't be used based on the selected [application access](#application-access).

> [!WARNING]
>
> A CCG app with App Access Only can send in the `box_subject_type` of `enterprise` to authenticate as its Service Account, but it can't authenticate as a Managed User or an Admin.

* Your application isn't configured to generate user access tokens. Enable **Generate User Access Tokens** in the **Advanced Features** section of the **Configuration** tab.

  ![Generate access tokens check](https://mintcdn.com/box/biE81cmEmV1QnYl4/guides/authentication/client-credentials/image.png?fit=max\&auto=format\&n=biE81cmEmV1QnYl4\&q=85\&s=eb49d9cdb4654e44e101e877baa60ef7)

> [!WARNING]
>
> Once you make changes to the app settings, don't forget to reauthorize the application in the Admin Console.

* Your application has not been authorized in the Box Admin Console.

## Client Credentials Grant with the Node SDK

Create a Box client with Client Credentials Grant, upload a file, and download that file.

> [tab] Service Account
>
> ```typescript Node/TypeScript v10 theme={null}
> import { BoxClient } from 'box-node-sdk';
> import { BoxCcgAuth, CcgConfig } from 'box-node-sdk/box';
>
> const ccgConfig = new CcgConfig({
>   enterpriseId: 'YOUR_ENTERPRISE_ID',
>   clientId: 'YOUR_CLIENT_ID',
>   clientSecret: 'YOUR_CLIENT_SECRET',
> });
> const ccgAuth = new BoxCcgAuth({ config: ccgConfig });
> const client = new BoxClient({ auth: ccgAuth });
>
> const fs = require('fs');
>
> const attrs = { name: 'filename.txt', parent: { id: '0' } };
> const body = {
>   attributes: attrs,
>   file: fs.createReadStream('filename.txt'),
> };
> const files = await client.uploads.uploadFile(body);
> const file = files.entries[0];
>
> const fileContent = await client.downloads.downloadFile(file.id);
> const fileWriteStream = fs.createWriteStream('file.pdf');
> fileContent.pipe(fileWriteStream);
> ```

> [tab] App User
>
> ```typescript Node/TypeScript v10 theme={null}
> import { BoxClient } from 'box-node-sdk';
> import { BoxCcgAuth, CcgConfig } from 'box-node-sdk/box';
>
> const ccgConfig = new CcgConfig({
>   userId: 'YOUR_USER_ID',
>   clientId: 'YOUR_CLIENT_ID',
>   clientSecret: 'YOUR_CLIENT_SECRET',
> });
> const ccgAuth = new BoxCcgAuth({ config: ccgConfig });
> const client = new BoxClient({ auth: ccgAuth });
>
> const fs = require('fs');
>
> const attrs = { name: 'filename.txt', parent: { id: '0' } };
> const body = {
>   attributes: attrs,
>   file: fs.createReadStream('filename.txt'),
> };
> const files = await client.uploads.uploadFile(body);
> const file = files.entries[0];
>
> const fileContent = await client.downloads.downloadFile(file.id);
> const fileWriteStream = fs.createWriteStream('file.pdf');
> fileContent.pipe(fileWriteStream);
> ```

[devconsole]: https://app.box.com/developers/console

[devaccount]: https://account.box.com/signup/n/developer

[cors]: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

**RELATED APIS**

**RELATED GUIDES**
