---
title: "Authentication"
description: "Get a token with the OAuth 2.0 client credentials flow."
---

The CloudLabs API accepts Microsoft Entra ID access tokens issued to your API client with the **client credentials** flow. Your system signs in as itself; no person is involved.

## Request a token

Use the token URL and scope shown when you created the client. The scope is always the application ID URI followed by `/.default`.

:::code-group
```bash title="cURL"
curl -s -X POST "https://login.microsoftonline.com/$TENANT_ID/oauth2/v2.0/token" \
  -d grant_type=client_credentials \
  -d client_id="$CLIENT_ID" \
  -d client_secret="$CLIENT_SECRET" \
  -d scope="api://$API_APP_ID/.default"
```
```powershell title="PowerShell"
$body = @{
  grant_type    = "client_credentials"
  client_id     = $env:CLIENT_ID
  client_secret = $env:CLIENT_SECRET
  scope         = "api://$($env:API_APP_ID)/.default"
}
$token = (Invoke-RestMethod -Method Post -Body $body `
  -Uri "https://login.microsoftonline.com/$($env:TENANT_ID)/oauth2/v2.0/token").access_token
```
:::

The response holds an `access_token` and its lifetime in `expires_in` (seconds). Reuse the token until shortly before it expires; don't request one per call.

## Call the API

```bash title="cURL"
curl -s "https://vnext-api.cloudlabs.ai/v1/labs/$LAB_ID" \
  -H "Authorization: Bearer $ACCESS_TOKEN"
```

:::info[Scopes are set in CloudLabs]
You don't ask for `labs.read` or other CloudLabs scopes in the token request. They belong to the client in CloudLabs, and the API checks them on every call.
:::

## Safe-retry creates

Calls that create something accept an `Idempotency-Key` header. Send a unique value (a UUID) per intended create; if the network fails, repeat the call with the same key and CloudLabs won't create a second copy.

:::warning
Keep the client secret on your server. Never put it in a browser, a mobile app or a repository.
:::
