# Client Credentials Flow

For applications like CLIs, daemons, lambda functions or services running on your backend, the system authenticates and authorizes the app rather than a user. Machine-to-machine (M2M) apps use the Client Credentials Flow in which they pass along their Client ID and Client Secret to authenticate themselves and get a token.

## How it works

```mermaid
sequenceDiagram
    M2M App->>Ninja Authorization Server: Authenticate with Client ID + Client Secret to /oauth/token
    Ninja Authorization Server-->Ninja Authorization Server: Validate Client ID + Client Secret
    Ninja Authorization Server->>M2M App: Access Token
    M2M App->>Ninja Public API: Request with Access Token
    Ninja Public API->>M2M App: Response
```

## How to implement it

### Prerequisites

Register your app in the NinjaOne application. To learn more, read [**Register Machine-to-Machine Applications**](/apidocs-beta/authorization/create-applications/machine-to-machine-apps).

- Select **API Services** as the Application Platform.
- Select the **scope(s)** your application will have access to.
- Make sure your application's Grant Types include **Client Credentials**.

### Request tokens

To access the Public API, you must request an Access Token for it. To do so, you will need to POST to the `/oauth/token` URL.

**Parameters**

| Name            | Description                                                                                                                                                      |
| --------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `grant_type`    | Set this to `client_credentials`.                                                                                                                                |
| `client_id`     | Your application's Client ID.                                                                                                                                    |
| `client_secret` | Your application's Client Secret.                                                                                                                                |
| `scope`         | A space-separated list of application scopes that you’d like access for. This defaults to all of the scopes configured for the application in its configuration. |

**Try out POST to token**

```httpRequest
{
  "method": "post",
  "url": "/oauth/token",
  "baseUrl": "/ws",
  "headers": {
    "Content-Type": "application/x-www-form-urlencoded"
  },
  "body": {
    "grant_type": "client_credentials",
    "client_id": "",
    "client_secret": "",
    "scope": ""
  }
}
```

**Response**

If all goes well, you'll receive an HTTP 200 response with a payload containing `access_token`, `expires_in`, `scope` and `token_type` values:

```json
{
  "access_token": "Qu5h5FzU9s_Dz7n4ZwJ5bWjg3CcupFW8lO7F_rPAsU4.N24zCN5F0ix3xZAsII0QomA7HALN1grDDbeyGCG0aOj",
  "expires_in": 3600,
  "scope": "monitoring",
  "token_type": "Bearer"
}
```
