---
title: "Trade for a user"
url: https://docs.truemarkets.co/gateway/trade-for-a-user
description: "Add TM-On-Behalf-Of to a request made with your organization token, and it runs for that user."
---

Docs index: https://docs.truemarkets.co/llms.txt

# Trade for a user

To trade for a user, make the request with your organization token and add `TM-On-Behalf-Of: <user_id>`. The request runs against that user's wallet, and everything else about it stays the same.

## How an order runs

Place the order, sign each payload with your signer key, execute, then read the order until its status is final. [Place orders for a user](https://docs.truemarkets.co/gateway/place-orders) walks through each call, and [Getting started](https://docs.truemarkets.co/gateway/quickstart#5-place-the-order-and-execute) shows the signing code.

## Send the header

The value is the `user_id` you got from [User accounts](https://docs.truemarkets.co/gateway/create-a-user). Send the header once per request. In code, keep it in one object and pass it to every call for that user, as the `post()` and `get()` helpers from [Getting started](https://docs.truemarkets.co/gateway/quickstart) do. The request uses your organization's permissions and acts only on that user's wallet.

**TypeScript**

```typescript title="An order for one of your users"
const forUser = { "TM-On-Behalf-Of": userId };

const order = await post(
  "/v1/gateway/orders",
  {
    asset_id: assetId,
    qty: "2",
    qty_unit: "quote",
    side: "buy",
    type: "market",
  },
  forUser,
);
```

**Python**

```python title="An order for one of your users"
for_user = {"TM-On-Behalf-Of": user_id}

order = post(
    "/v1/gateway/orders",
    {
        "asset_id": asset_id,
        "qty": "2",
        "qty_unit": "quote",
        "side": "buy",
        "type": "market",
    },
    for_user,
)
```

**curl**

```bash title="An order for one of your users"
curl -s -X POST https://api.truemarkets.co/v1/gateway/orders \
  -H "Authorization: Bearer $ORG_TOKEN" \
  -H "TM-On-Behalf-Of: $USER_ID" \
  -H "Content-Type: application/json" \
  -d '{
    "asset_id": "'"$ASSET_ID"'",
    "qty": "2",
    "qty_unit": "quote",
    "side": "buy",
    "type": "market"
  }'
```

## Where it works

These are the calls you use most. The [API reference](https://docs.truemarkets.co/api/gateway/trading-rest) marks the header on every endpoint that accepts it.

| Route                                               | Header                                       |
| --------------------------------------------------- | -------------------------------------------- |
| `/orders` and every route under it                  | accepted                                     |
| `/transfers` and every route under it               | accepted                                     |
| `GET /balances`, `GET /portfolio`, `GET /positions` | accepted                                     |
| `GET /transactions`                                 | accepted                                     |
| `GET /organizations/{organization_id}/transactions` | rejected: the feed already covers every user |

## When it fails

| Status                            | Meaning                                                                                                                                                               |
| --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `400`                             | You sent the header with a Trading API token instead of an organization token, sent it twice, or the value isn't a UUID.                                              |
| `401`                             | The token is missing, invalid or expired, or you called a user route with an organization token and no header.                                                        |
| `403`                             | The user isn't in your organization, or trading that asset isn't available in the country your request comes from. `GET /assets/availability` shows what's available. |
| `201` with an empty `order_id`    | The wallet can't fund the buy. `quote.issues` says why, and no order was created.                                                                                     |
| `422` with `insufficient_balance` | The wallet can't fund a sell or a perpetual order.                                                                                                                    |
| `422` with `quote_stale`          | The quote expired or the price moved before execute landed. Place the order again.                                                                                    |

[Errors](https://docs.truemarkets.co/developer-resources/errors) shows the error body and which errors to retry. The [API reference](https://docs.truemarkets.co/api/gateway/trading-rest) lists every code for each endpoint.

Next: [Place orders for a user](https://docs.truemarkets.co/gateway/place-orders)