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

# Generating an Access Token

> Generate and refresh OAuth2 access tokens for FEX API calls.

All API calls require a **Bearer token** obtained via the OAuth 2.0 client credentials flow. Tokens expire after **5 minutes** — implement automatic refresh in your backend.

### Request

```bash theme={null}
curl -X POST https://auth-sandbox.fexpayments.com/realms/payment-platform/protocol/openid-connect/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=client_credentials" \
  -d "client_id=merchant-a1b2c3d4-..." \
  -d "client_secret=xxxxxxxxxxxxxxxx"
```

### Response

```json theme={null}
{
  "access_token": "eyJhbGciOiJSUzI1NiIs...",
  "expires_in": 300,
  "token_type": "Bearer"
}
```

### Token Caching (Recommended Pattern)

Request a new token before each API call, or cache and refresh proactively using the pattern below:

```python theme={null}
if token is None or token_expires_at < now() + 30s:
    token = fetch_new_token(client_id, client_secret)
    token_expires_at = now() + token.expires_in
```

> **Important:** Always refresh at least 30 seconds before expiry to avoid race conditions near the 5-minute boundary.

<br />

***

<Frame>
  <img src="https://mintcdn.com/fexpayments-0eb1bb58/LW18DimKRuYbg8YT/images/image-1.png?fit=max&auto=format&n=LW18DimKRuYbg8YT&q=85&s=450746fb08479ac01fd471d05e714dbb" alt="Image" width="906" height="216" data-path="images/image-1.png" />
</Frame>
