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

# Authentication

> Call the API for your own AGS account with a personal access token, or for your customers with OAuth 2.0.

How you authenticate depends on whose account the orders belong to:

| You're placing orders for | Use |
| - | - |
| Your own AGS account | A [personal access token](#personal-access-tokens) |
| Your customers, each with their own AGS account | The [OAuth 2.0 authorization code flow](#connect-a-customer) |

Either way, every API request sends the token as a bearer token:

```
Authorization: Bearer ACCESS_TOKEN
```

## Personal access tokens

AGS issues a personal access token for your AGS account, with the scopes your integration needs. Send it as the bearer token. There's no sign-in flow and nothing to refresh. Orders you create belong to your account, and you see the orders placed with any of your account's tokens.

> [!WARNING]
>
> The token is shown once, when it's created. Keep it on your server, like a password, and never ship it in a mobile or browser app.

Tokens last one year. To replace a token, ask AGS for a new one, switch your integration to it, then ask AGS to revoke the old one. A revoked or expired token gets a `401` with the `unauthenticated` error code.

## OAuth client credentials

AGS registers your app and gives you its credentials. Tell us your app's redirect URIs when you register.

| App type | Credentials | Use for |
| - | - | - |
| Confidential | Client ID and client secret | Apps with a server that can keep the secret private |
| Public | Client ID only | Mobile and browser-only apps, which can't keep a secret |

> [!WARNING]
>
> The client secret is shown once, when it's created. Store it securely on your server and never ship it in a mobile or browser app.

Redirect URIs must match exactly. Mobile apps can use a custom scheme, such as `yourapp://callback`.

## Scopes

| Scope | Allows |
| - | - |
| `orders:read` | Viewing the orders you created through the API, and the reference data used to create orders |
| `orders:write` | Creating orders, and the reference data used to create orders |

Request scopes as a space-separated list. If you request none, the token gets `orders:read`. Each endpoint in the API reference lists the scope it needs, and a token without it gets a `403` with the `insufficient_scope` error code.

## Connect a customer

**1. Create a PKCE code verifier and challenge**

Generate a random `code_verifier` of 43 to 128 characters, and derive the `code_challenge` as the base64url-encoded SHA-256 hash of it. PKCE is required for public apps, and we recommend it for confidential apps too.

**2. Send the customer to the authorize URL**

```
https://app.agscard.com/oauth/authorize
  ?response_type=code
  &client_id=CLIENT_ID
  &redirect_uri=https://yourapp.example.com/callback
  &scope=orders:read orders:write
  &state=RANDOM_STATE
  &code_challenge=CODE_CHALLENGE
  &code_challenge_method=S256
```

The customer signs in to AGS if needed, then sees which app is asking and what it will be able to do.

**3. Handle the redirect**

If the customer approves, AGS redirects to your `redirect_uri` with `code` and `state` query parameters. Check that `state` matches the value you sent. If the customer denies access, the redirect carries `error=access_denied` instead.

The code expires after 10 minutes and can be used once.

**4. Exchange the code for tokens**

```bash Confidential app theme={null}
curl -X POST https://app.agscard.com/oauth/token \
  -d grant_type=authorization_code \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d redirect_uri=https://yourapp.example.com/callback \
  -d code_verifier=CODE_VERIFIER \
  -d code=AUTHORIZATION_CODE
```

```bash Public app theme={null}
curl -X POST https://app.agscard.com/oauth/token \
  -d grant_type=authorization_code \
  -d client_id=CLIENT_ID \
  -d redirect_uri=yourapp://callback \
  -d code_verifier=CODE_VERIFIER \
  -d code=AUTHORIZATION_CODE
```

The response contains the tokens:

```json theme={null}
{
  "token_type": "Bearer",
  "expires_in": 3600,
  "access_token": "ACCESS_TOKEN",
  "refresh_token": "REFRESH_TOKEN"
}
```

## Refresh the access token

Access tokens last one hour. Use the refresh token to get a new pair before or after the access token expires:

```bash theme={null}
curl -X POST https://app.agscard.com/oauth/token \
  -d grant_type=refresh_token \
  -d client_id=CLIENT_ID \
  -d client_secret=CLIENT_SECRET \
  -d refresh_token=REFRESH_TOKEN
```

Public apps leave out `client_secret`.

Refresh tokens last 30 days and can be used once. Each refresh returns a new refresh token, so store it in place of the old one.

> [!NOTE]
>
> Errors from `/oauth/authorize` and `/oauth/token` follow the OAuth 2.0 format, with `error` and `error_description` fields. API endpoints use the format in [Errors](https://docs.agscard.com/errors.md).

## When tokens stop working

With OAuth, a customer's tokens are revoked, and your app has to connect them again, when:

* The refresh token expires after 30 days without use.
* The customer changes their AGS password.
* AGS revokes your app's client.

API requests with a revoked or expired access token get a `401` with the `unauthenticated` error code.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.