# Accessing the API

> Learn how to access the Untether Labs API

The Untether Labs API uses OAuth 2.0 `client_credentials` grant for machine to machine authentication.
You may read more information on this flow [here](https://www.oauth.com/oauth2-servers/access-tokens/client-credentials/).

## Obtaining Credentials

API access must be enabled for your company. If it isn't yet, please [reach out](mailto:support@untetherlabs.com).

Once it is, an admin can create API clients in [Company settings > API clients](https://app.untetherlabs.com/company-settings/api-clients):

1. Click **New API client**.
2. Name it after the integration that will use it, then click **Create**.
3. Copy the **Client ID** and **Client secret**. The secret is shown only once; if you lose it,
   [rotate it](#rotating-the-client-secret) to get a new one.

- **Client ID**: A public identifier for your application.
- **Client Secret**: A secret known only to your application and the authorization server.

<Note>
    Sandboxes have their own API clients. Create them on the same page while signed in to
    the sandbox.
</Note>

## Access Token Exchange

The Client ID and Client Secret do not provide access to the API by themselves, you must
exchange them for a short-lived access token. This is accomplished by sending them to the
`/oauth2/token` endpoint:

```bash Token Request icon=file-terminal
curl -X POST --location "https://auth.untetherlabs.com/oauth2/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d 'grant_type=client_credentials&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}'
```

```json Token Response icon=braces
{
    "access_token": "{ACCESS_TOKEN}",
    "token_type": "Bearer",
    "expires_in": 3600,
    "expires_at": 1793190145
}
```

`expires_in` is the token's lifetime in seconds, and `expires_at` is when it expires as a Unix
timestamp in seconds.

<Accordion title="If you have a legacy API client">

Clients that were created for you by Untether support, before API clients could be managed in
company settings, exchange their credentials at a different URL and receive a slightly different
response:

```bash Legacy Token Request icon=file-terminal
curl -X POST --location "https://app.untetherlabs.com/api/oauth/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d 'grant_type=client_credentials&client_id={CLIENT_ID}&client_secret={CLIENT_SECRET}'
```

```json Legacy Token Response icon=braces
{
    "access_token": "{ACCESS_TOKEN}",
    "token_type": "Bearer",
    "expires_at": "{ISO TIMESTAMP}"
}
```

Legacy clients keep working, and there is no end-of-life date for them yet. When there is one, we
will announce it well in advance and send reminders before it. To migrate:

1. Create a new client in [Company settings > API clients](https://app.untetherlabs.com/company-settings/api-clients).
2. Switch your token URL from `https://app.untetherlabs.com/api/oauth/token` to
   `https://auth.untetherlabs.com/oauth2/token`.

</Accordion>

## Your First Request

With an access token, you are now free to make requests to the API any time before `expires_at`.

For example, we can fetch our own user information:

```bash First API Request icon=file-terminal
curl -X GET --location "https://app.untetherlabs.com/api/v1/users/{CLIENT_ID}" \
    -H "Authorization: Bearer {ACCESS_TOKEN}"
```

```json Get User Response icon=braces
{
    "id": "{CLIENT_ID}",
    "type": "bot",
    "name": "My API Client",
    "email": "api@untetherlabs.com",
    "status": "active"
}
```

<Info>
    For more information about this endpoint, see `GET /v1/users/{userId}` in the API
    reference.
</Info>

## Rotating the Client Secret

To replace a client's secret, for example if it may have leaked or you have lost it, open
[Company settings > API clients](https://app.untetherlabs.com/company-settings/api-clients), choose
**Rotate secret** from the client's menu and copy the new secret. Like a new client's, it is shown
only once.

The old secret stops working immediately, so anything still using it cannot get new access tokens
until it is updated. Access tokens it already obtained keep working until they expire, within the
hour.
