# Authenticate the Shipping Assistant's calls

Implement POST /auth for the Kustom Shipping Assistant: verify the sha256 nonce digest, return a bearer token and answer 401 to make Kustom fetch a new one.

Implement `POST /auth` to issue the bearer token Kustom uses for calls to your TMS. Kustom
authenticates with a digest of the merchant's key instead of sending the key. Without a token,
Kustom makes no further calls and checkout uses the merchant's fallback options.

## Credentials

You issue each merchant two values, and the merchant enters them in the Kustom Portal:

| Portal field | Sent as | What it is |
| --- | --- | --- |
| **Identifier** | `identifier` | Which merchant account this is, in your system |
| **Key** | never sent | A shared secret. Kustom sends only a hash of it |

The profile also names your TMS, which is how Kustom knows your base URL. Kustom appends
`/auth`, `/shippingoptions` and `/shipment` to that one URL.

## Request

```http
POST /auth
Content-Type: application/vdn.klarna.shipping.auth-v1+json
```

```json
{
  "identifier": "merchant-4711",
  "secret": {
    "nonce": "n8Jq2LwXv0ZbT5mA",
    "digest": "8f9fc1128e9dc6e85877176a982100f0c8cea09352bfe12e9ff988d8af38eb63"
  }
}
```

| Field | Meaning |
| --- | --- |
| `identifier` | The Identifier from the merchant's profile |
| `secret.nonce` | 16 random letters and digits, new on every call |
| `secret.digest` | SHA-256 of the nonce followed by the key, as UTF-8, written as **lowercase** hex. The example is the digest for the key `example-key` |

The content type contains `vdn`, not `vnd`. If you validate it, accept it as written.

To check the digest, compute the same hash from your stored key and compare. Compare without
regard to case: Kustom sends lowercase, but the API reference's example is uppercase and some
older integrations were written against that.

```js
import { createHash, timingSafeEqual } from 'node:crypto';

function digestMatches(nonce, digest, key) {
  const expected = createHash('sha256').update(nonce + key, 'utf8').digest('hex');
  const received = digest.toLowerCase();
  return received.length === expected.length &&
    timingSafeEqual(Buffer.from(received), Buffer.from(expected));
}
```

## Response

Answer `200` or `201` with the token:

```json
{ "token": "eyJhbGciOiJIUzI1NiJ9.bWVyY2hhbnQtNDcxMQ.k3M2..." }
```

| Field | Required | Meaning |
| --- | --- | --- |
| `token` | **Yes** | Sent back to you as `Authorization: Bearer <token>` on every other call |

Other response fields, including `expires_in`, are ignored. A response without `token`, or with
any status other than `200` or `201`, is a failed authentication, and the checkout falls back to
the merchant's options. Answer `401` for wrong credentials so the failure is easy to read in your
own logs.

## How Kustom keeps the token

Kustom calls `POST /auth` when it needs a token, usually before the first
`POST /shippingoptions`. It stores the token per merchant profile and reuses it across
checkouts for up to a day. To expire a token:

1. Answer `401` to any call whose token you no longer accept.
2. Kustom drops the token, calls `POST /auth` again and repeats the call once with the new token.
3. If that repeat also gets `401`, the call fails, the same as any other error.

Choose your own expiry schedule, for example one hour. Issue a separate token per merchant
so you can revoke one without affecting others.

## Headers on every call

| Header | Value |
| --- | --- |
| `Authorization` | `Bearer <token>`, on every call except `POST /auth` |
| `Content-Type` | Per call, on calls with a body — see below |
| `Klarna-Correlation-Id` | Kustom's id for the request, when there is one. Log it, and quote it when you contact Kustom about a call |

| Call | `Content-Type` |
| --- | --- |
| `POST /auth` | `application/vdn.klarna.shipping.auth-v1+json` |
| `POST /shippingoptions` | `application/vdn.klarna.shipping.get_options-v1+json` |
| `POST /shipment`, `PUT /shipment/{shipment_id}` | `application/vdn.klarna.shipping.select_option-v1+json` |

Kustom sends no `Accept` header and no idempotency key. Respond with JSON.
Fields are snake_case in both directions, and Kustom ignores fields it doesn't recognize in your
responses.

## Related

- [Return shipping options](/checkout/shipping/shipping-assistant/shipping-options/) — the first call that uses the token
- [`POST /auth` in the API reference](/api/shipping-service-callback/)
- [Build a TMS integration](/checkout/shipping/shipping-assistant/) — where authentication sits in the flow
