# Authentication

Source: https://docs.globagift.io/docs/api/authentication
Description: Authenticate to the Globagift API with merchant or distributor keys, choose scopes, and sign redemption requests from acceptance points.


The API uses three kinds of credential. Each one is shown in full only once, when it is created. Keep them on your servers and out of browsers, apps and source control.

| Credential | Looks like | Sent as | Created by |
| --- | --- | --- | --- |
| Merchant API key | `gg_...` | `Authorization: Bearer` header | The merchant, in the portal or with the API |
| Distributor key | `ggd_...` | `Authorization: Bearer` header | Globagift, during distributor onboarding |
| Acceptance-point credential | An id and a secret | Four signed `gg-` headers | The merchant, with the API |

## Merchant API keys [#merchant-api-keys]

A merchant key acts for one brand. Create keys in the merchant portal under **Integrations**, **API access**, or with [`POST /v1/api-keys`](/docs/api/reference/api-keys) using a key that has `keys:manage`.

Keys are either **test** or **live**. Test keys work in the sandbox. Live keys become available once the brand is approved for live use.

```bash
curl -s "https://engine-sandbox.globagift.io/v1/me" \
  -H "Authorization: Bearer gg_your_test_key"
```

### Scopes [#scopes]

Give each key only the scopes its integration needs, and use a separate key for each integration so you can revoke one without affecting the others.

| Scope | Allows |
| --- | --- |
| `products:read` | List and read card products |
| `products:write` | Create and update card products |
| `cards:issue` | Issue cards, and read products and cards while issuing |
| `cards:read` | List and read cards, and look up a card by redemption code |
| `cards:manage` | Suspend, release and void cards |
| `redemptions:read` | List redemptions |
| `reports:read` | Activity, outstanding balance and ageing reports |
| `webhooks:read` | List webhook subscriptions and events |
| `webhooks:write` | Create and delete webhook subscriptions |
| `acceptance:manage` | Create acceptance points and issue or revoke their credentials |
| `keys:manage` | Create, list and revoke API keys |

A request with a valid key but without the right scope returns `403` with code `insufficient_scope`.

Revoke a key with [`DELETE /v1/api-keys/{id}`](/docs/api/reference/api-keys) or in the portal. It stops working straight away.

## Distributor keys [#distributor-keys]

A distributor key acts for the distributor, not for any merchant. It carries one of two scopes:

| Scope | Allows |
| --- | --- |
| `distribution:read` | List granted merchants and their products, look up orders and read statements |
| `distribution:sell` | Everything in `distribution:read`, plus reserve, capture and release |

Globagift creates distributor keys during onboarding. See [Distributor marketplace](/docs/api/distributor).

## Signed requests from acceptance points [#signed-requests-from-acceptance-points]

Redemptions do not use an API key. They are signed by an **acceptance point**: a till, site or app where cards are spent. This keeps redemption authority with the place that takes the card, not with every system that holds a key.

Set one up with a merchant key that has `acceptance:manage`:

1. Create the acceptance point with [`POST /v1/acceptance-points`](/docs/api/reference/acceptance-points), giving a `name` and a `channel` of `store`, `ecommerce` or `app`.
2. Issue a credential with `POST /v1/acceptance-points/{id}/credentials`. The response holds the credential `id` and its `secret`. The secret is shown once.

### Build the signature [#build-the-signature]

Every signed request carries four headers:

| Header | Value |
| --- | --- |
| `gg-credential` | The credential id |
| `gg-timestamp` | The current Unix time in seconds |
| `gg-nonce` | A new random value for every request, such as a UUID |
| `gg-signature` | The signature, in lowercase hex |

To build `gg-signature`:

1. Take the exact raw request body you will send, and compute its SHA-256 in lowercase hex.
2. Join these five values with a newline (`\n`) between each: the upper-case method, the path, the timestamp, the body hash and the nonce.
3. Compute HMAC-SHA256 of that string, keyed with the credential secret, and encode it in lowercase hex.

```js title="sign.mjs"
import { createHash, createHmac, randomUUID } from 'node:crypto';

export function signedHeaders({ credentialId, secret, method, path, body }) {
  const timestamp = String(Math.floor(Date.now() / 1000));
  const nonce = randomUUID();
  const bodyHash = createHash('sha256').update(body).digest('hex');
  const canonical = [method.toUpperCase(), path, timestamp, bodyHash, nonce].join('\n');
  const signature = createHmac('sha256', secret).update(canonical).digest('hex');
  return {
    'gg-credential': credentialId,
    'gg-timestamp': timestamp,
    'gg-nonce': nonce,
    'gg-signature': signature,
  };
}
```

Sign the same string you send. If your HTTP client re-serialises JSON, the body hash will not match.

### When a signature is refused [#when-a-signature-is-refused]

Each of these returns `401` with code `invalid_signature`:

- the signature does not match the body, path or secret;
- the timestamp is more than five minutes from the server's clock;
- the nonce has already been used with this credential;
- the credential has been revoked.

The response does not say which, so check the clock on the device and sign each attempt afresh. To retry a redemption, see [Retries and idempotency](/docs/api/conventions#retries-and-idempotency).

Revoke a credential with `DELETE /v1/acceptance-points/{id}/credentials/{credential_id}` when a device is lost or retired.
