# Exchange a connection code

> Exchanges the one-time code from the TapPay Connect redirect for the keys of a plugin.

Source: https://docs.tappay.mn/reference/connect/token/

Your plugin server exchanges the one-time code from the TapPay Connect redirect, with its PKCE verifier, for a [secret key](https://docs.tappay.mn/glossary.md#secret-key), a signing secret and a [webhook secret](https://docs.tappay.mn/glossary.md#webhook-secret). TapPay returns the secrets only once, so store them right away.

`POST /api/v1/connect/token` Since 2026-09-27

### Authentication

Secret key (Bearer)

### Request body

| Name | Type | Required | Values | Description |
| - | - | - | - | - |
| `code` | string | yes | | |
| `codeVerifier` | string | yes | | |
| `redirectUri` | string | yes | | |

### Response 200

| Name | Type | Required | Values | Description |
| - | - | - | - | - |
| `success` | boolean | yes | `true` | |
| `data` | object | yes | | |
| `data.secretKey` | string | yes | | |
| `data.signingSecret` | string | yes | | |
| `data.webhookSecret` | string | yes | | |
| `data.webhookEndpointId` | string (uuid) | yes | | |
| `data.connectionId` | string (uuid) | yes | | |
| `data.clientSystemId` | string (uuid) | yes | | |
| `data.merchantId` | string (uuid) | yes | | |
| `data.merchant` | object | yes | | |
| `data.merchant.id` | string (uuid) | yes | | |
| `data.merchant.name` | string | yes | | |
| `data.gateHubBusinessId` | string \| null | yes | | |
| `data.terminalId` | string \| null | yes | | |
| `data.environment` | string | yes | `SANDBOX`, `PRODUCTION` | |
| `data.stickerSecret` | string \| null | yes | | |

### Errors

| Code | Status | Message |
| - | - | - |
| [`VAL-001`](https://docs.tappay.mn/errors.md#VAL-001) | 400 | The request failed validation. |
| [`AUTH-075`](https://docs.tappay.mn/errors.md#AUTH-075) | 400 | The connection code is invalid, expired or already used. |
| [`AUTH-077`](https://docs.tappay.mn/errors.md#AUTH-077) | 409 | The maximum number of active connections has been reached. |
| [`BIZ-005`](https://docs.tappay.mn/errors.md#BIZ-005) | 403 | The merchant account is not in a state that allows this request. |
| [`BIZ-013`](https://docs.tappay.mn/errors.md#BIZ-013) | 404 | The terminal was not found. |
| [`EVT-002`](https://docs.tappay.mn/errors.md#EVT-002) | 422 | The webhook endpoint limit has been reached. |
| [`EVT-004`](https://docs.tappay.mn/errors.md#EVT-004) | 422 | The webhook endpoint URL must use HTTPS. |

Try it in the playground: https://docs.tappay.mn/reference/connect/token/

```bash title="curl"
curl -sS -X POST "https://api.tappay.mn/api/v1/connect/token" \
  -H "Authorization: Bearer sk_test_YOUR_SECRET_KEY" \
  -H "TapPay-Version: 2026-09-27" \
  -H "Idempotency-Key: $(openssl rand -hex 16)" \
  -H "Content-Type: application/json" \
  -d '{"code":"CONNECT_CODE","codeVerifier":"PKCE_CODE_VERIFIER","redirectUri":"https://shop.example.com/tappay/callback"}'
```

```js title="Node.js"
import { TapPay } from '@tappay-mn/node';

const tappay = new TapPay({ apiKey: 'sk_test_YOUR_SECRET_KEY' });
const result = await tappay.connect.exchangeCode({
  body: {
    code: 'CONNECT_CODE',
    codeVerifier: 'PKCE_CODE_VERIFIER',
    redirectUri: 'https://shop.example.com/tappay/callback',
  },
});
console.log(JSON.stringify(result));
```

```php title="PHP"
<?php
require 'vendor/autoload.php';

use TapPay\TapPay;

$tappay = new TapPay(['apiKey' => 'sk_test_YOUR_SECRET_KEY']);
$result = $tappay->connect->exchangeCode([
    'body' => [
        'code' => 'CONNECT_CODE',
        'codeVerifier' => 'PKCE_CODE_VERIFIER',
        'redirectUri' => 'https://shop.example.com/tappay/callback',
    ],
]);
echo json_encode($result);
```

```python title="Python"
import json
import tappay

client = tappay.TapPay("sk_test_YOUR_SECRET_KEY")
result = client.connect.exchange_code({
    "body": {
        "code": "CONNECT_CODE",
        "codeVerifier": "PKCE_CODE_VERIFIER",
        "redirectUri": "https://shop.example.com/tappay/callback",
    },
})
print(json.dumps(result))
```

```json title="Response"
{
  "success": true,
  "data": {
    "secretKey": "sk_test_YOUR_SECRET_KEY",
    "signingSecret": "YOUR_SIGNING_SECRET",
    "webhookSecret": "YOUR_WEBHOOK_SECRET",
    "webhookEndpointId": "0192f8a4-1b2c-7d3e-8f40-5a6b7c8d9f0c",
    "connectionId": "0192f8a4-1b2c-7d3e-8f40-5a6b7c8d9f0d",
    "clientSystemId": "0192f8a4-1b2c-7d3e-8f40-5a6b7c8d9f0d",
    "merchantId": "YOUR_MERCHANT_ID",
    "merchant": {
      "id": "YOUR_MERCHANT_ID",
      "name": "Sandbox Cafe"
    },
    "gateHubBusinessId": null,
    "terminalId": null,
    "environment": "SANDBOX",
    "stickerSecret": null
  }
}
```
