# Exchange a signature

Exchange the signed challenge for an access token

`POST https://auth.cadmos.finance/v1/external-auth/token`

A challenge is used once. Send the token as `Authorization: Bearer <accessToken>` to api.cadmospay.com only.

## Authentication

This endpoint accepts requests without a bearer token.

## Example requests

```bash
curl --fail-with-body 'https://auth.cadmos.finance/v1/external-auth/token' \
  -H "Content-Type: application/json" \
  --data-binary @signed-challenge.json
```

```javascript
import { readFile } from "node:fs/promises";

const url = new URL("https://auth.cadmos.finance/v1/external-auth/token");
const body = await readFile("signed-challenge.json");
const response = await fetch(url, {
  method: "POST",
  redirect: "error",
  headers: {
    "Content-Type": "application/json",
  },
  body,
});
console.log(response.status, await response.json());
```

```python
from http.client import HTTPSConnection
from pathlib import Path

conn = HTTPSConnection("auth.cadmos.finance", timeout=30)
path = "/v1/external-auth/token"
body = Path("signed-challenge.json").read_bytes()
headers = {
    "Content-Type": "application/json",
}
conn.request("POST", path, body=body, headers=headers)
response = conn.getresponse()
print(response.status, response.read().decode())
conn.close()
```

## Request body

`application/json`

[TokenRequest](https://cadmospay.com/developers/schemas/token-request.md)

Only the listed properties are accepted.

- `challengeId` (string, required, format uuid). 
- `message` (string, required, length at most 2000). The challenge's message, unchanged.
- `signature` (string, required, pattern ^0x[0-9a-fA-F]{130}$). Your EIP-191 signature of the message.

## Responses

### 200

The access token.

[Token](https://cadmospay.com/developers/schemas/token.md)

```json
{
  "accessToken": "<access token>",
  "tokenType": "Bearer",
  "expiresIn": 900,
  "identityId": "a08cccbc-f085-4289-bd48-9b2fb55acd14"
}
```

### 400

Malformed request.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 401

Unknown or disabled identity or address, wrong signature, or a challenge already used or expired.

[Error](https://cadmospay.com/developers/schemas/error.md)

### 503

Sign-in is unavailable. Retry later.

[Error](https://cadmospay.com/developers/schemas/error.md)
