> ## Documentation Index
> Fetch the complete documentation index at: https://takeprofit.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Trading keys and signing

> Create a Platform API key with the trading scope, keep its private key, and sign every Trading API request and stream with it.

The Trading API places real orders, so a key alone isn't enough. A trading key comes with a private key: TakeProfit keeps only the matching public key, and your code signs every request with the private one.

## Create a trading key

**1. Open token settings**

Open [takeprofit.com/settings/account](https://takeprofit.com/settings/account), go to **Login & Security** and scroll to **Personal API Tokens**. Click **Generate New Token**.

**2. Pick the trading scope**

Name the token, for example `grid-bot`, and tick **TRADING:ACCESS:WRITE**. Trading scopes can't be combined with other scopes, so keep a separate key for [market data](https://takeprofit.com/docs/platform-api/authentication.md).

**3. Set how long it lasts**

Under **Expires in**, pick **7**, **30** or **60 days**. A trading key always expires, and the default is 30 days. The list shows the date next to the key.

**4. Copy the token and the private key**

Click **Generate Token**. Your browser creates the key pair, and the dialog shows the token, the public key and the private key **only once**. Copy the token and the private key, and save the private key as a file, for example `trading-key.pem`. TakeProfit never receives the private key, so a lost one can't be recovered: create a new key.

Keep the private key out of your code and your repository, and use one key per script, so you can revoke one without stopping the others. To revoke a key, click **Revoke** next to it in **Personal API Tokens**. Open streams that use it end within 5 minutes.

## Sign every request

Send four headers with every REST request and every WebSocket handshake:

| Header | Value |
| - | - |
| `X-API-KEY` | The token, `tpk_…` |
| `X-API-TIMESTAMP` | The time of the request, in Unix milliseconds |
| `X-API-RECV-WINDOW` | Optional. How many milliseconds after the timestamp the request is still accepted: `1` to `60000`, `5000` by default |
| `X-API-SIGNATURE` | The Ed25519 signature, in hex, of `timestamp + apiKey + recvWindow + payload` |

The signed message joins the three header values exactly as sent, then the payload. An omitted `X-API-RECV-WINDOW` adds nothing. The payload is:

* **POST:** the raw request body, exactly as sent.
* **GET and WebSocket handshake:** the raw query string as sent, without `?`, or nothing when there is none.

Only the handshake is signed on a stream. Nothing is signed after it.

```python theme={null}
import os, time

from cryptography.hazmat.primitives.serialization import load_pem_private_key

API_KEY = os.environ["TAKEPROFIT_TRADING_KEY"]
with open("trading-key.pem", "rb") as f:
    PRIVATE_KEY = load_pem_private_key(f.read(), password=None)

def signed_headers(payload: str) -> dict:
    timestamp = str(int(time.time() * 1000))
    message = (timestamp + API_KEY + payload).encode()
    return {
        "X-API-KEY": API_KEY,
        "X-API-TIMESTAMP": timestamp,
        "X-API-SIGNATURE": PRIVATE_KEY.sign(message).hex(),
    }
```

Sign the exact bytes you send. If your HTTP library re-serialises the body or re-encodes the query string after you signed it, the signature no longer matches: build the body or query yourself, sign it, and send that string.

## Keep the clock in sync

A request is accepted while its timestamp is at most the receive window behind our server time and at most 1 second ahead of it. Sync your clock with NTP.

There is no nonce: within the window, a captured request can be sent again. Keep the window short, and use `Idempotency-Key` when placing orders so a repeat never places a second one.

## When a request is refused

| Status | Cause |
| - | - |
| `401` `unauthenticated` | A missing or malformed header, a timestamp outside the window, an unknown, revoked or expired key, a key created without a public key, or a wrong signature. The message names a header or clock problem, but never says whether the key or the signature was wrong. |
| `403` `permission_denied` | The key doesn't have the trading scope. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.