> Index of every page: https://docs.merchantservicedepot.com/llms.txt

# Exchange for a payment token


`POST /v1/tokens`

Exchanges the provider token the fields produced for an opaque `mtok_`, which is what `POST /v1/payments` accepts as `paymentToken`.

The session id is the capability — there is no key on this call beyond the publishable one. A session exchanges ONCE; a second attempt is refused, and so is a replay of the resulting `mtok_`.

Both variables come from the frame, so this request cannot be driven from the collection alone until js.merchantservicedepot.com is serving.

Auth: a signed-in session. Returns `201` with Payment token.

## Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `sessionId` | body | string | Yes | The short-lived checkout-session capability. |
| `upstreamToken` | body | string | Yes | The processor token exchanged inside the hosted Elements frame. |

## Request

**curl**

```curl
curl -X POST "https://payments-api-sandbox.merchantservicedepot.com/v1/tokens" \
  -H "Authorization: Bearer $MYELA_PUBLISHABLE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "sessionId": "{{CHECKOUT_SESSION_ID}}",
  "upstreamToken": "{{UPSTREAM_TOKEN}}"
}'
```

**Node**

```node
const res = await fetch("https://payments-api-sandbox.merchantservicedepot.com/v1/tokens", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.MYELA_PUBLISHABLE_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    "sessionId": "{{CHECKOUT_SESSION_ID}}",
    "upstreamToken": "{{UPSTREAM_TOKEN}}"
  }),
});

if (!res.ok) {
  const { error } = await res.json();
  // Branch on error.code — never on error.message.
  throw new Error(error.code);
}
const data = await res.json();
```

**Python**

```python
import os
import requests

res = requests.post(
    "https://payments-api-sandbox.merchantservicedepot.com/v1/tokens",
    headers={
        "Authorization": f"Bearer {os.environ['MYELA_PUBLISHABLE_KEY']}",
    },
    json={
        "sessionId": "{{CHECKOUT_SESSION_ID}}",
        "upstreamToken": "{{UPSTREAM_TOKEN}}"
    },
)

if not res.ok:
    # Branch on error["code"] — never on error["message"].
    raise RuntimeError(res.json()["error"]["code"])
data = res.json()
```

## Response

| Field | Type | Description |
| --- | --- | --- |
| `token` | string | Single-use Myela payment token. |

```json
{
  "token": "mtok_01JEXAMPLE"
}
```

Source: https://docs.merchantservicedepot.com/api-reference/exchange-for-a-payment-token/
