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

# Push customer revenue

> Send each customer's monthly revenue from your billing system to TensorCost with a signed request, so margins have revenue to work from.

A customer's margin is what they paid you less what their AI calls cost. TensorCost knows the cost. The revenue comes from you, and the way it comes in is a signed request that your billing job or script makes.

TensorCost never holds a credential to your billing, CRM or payment system. It has no Stripe key of yours and doesn't read your invoices. Your job reads its own system, works out each customer's month, and sends the figures. The only secret involved is a signing secret that TensorCost made for you, which lets your job write revenue to your own workspace and nothing else.

> [!NOTE]
>
> Available on Scale and Enterprise, including BYOC. Only an admin makes signing secrets. Below Scale, existing secrets can still be listed and revoked, but pushes are refused.

If you'd rather upload a file, [Cost per customer](https://docs.tensorcost.com/spend/customers.md#revenue-from-a-csv) takes a CSV. The two can be used together.

## What you send

Revenue is a **running monthly total** for one customer, one month and one currency. Each push says what the total is as of a time you choose, and the newest figure wins. Send the whole month so far, not the amount since your last push. A push replaces the figure; it never adds to it.

```json theme={null}
{
  "request_id": "revenue-2026-11-15T10-00",
  "as_of": "2026-11-15T10:00:00Z",
  "rows": [
    { "customer": "acme", "month": "2026-11", "revenue": "4210.00", "currency": "USD" },
    { "customer": "globex", "month": "2026-11", "revenue": "-35.50", "currency": "USD" }
  ]
}
```

| Field | Rule |
| - | - |
| `request_id` | Yours. 8 to 128 characters of letters, digits and `. _ : -`, starting with a letter or digit. |
| `as_of` | When the figures were true. ISO-8601 with an offset, such as `2026-11-15T10:00:00Z`. Not more than 5 minutes ahead of TensorCost's clock. |
| `rows` | 1 to 1,000 rows. |
| `customer` | The same id your calls carry in the `customer` tag. Up to 128 characters, case matters, no spaces at either end and no control characters. An id that looks like an email address is accepted with a warning, since ids are meant to be your own labels. |
| `month` | `YYYY-MM`. |
| `revenue` | A string in major units with at most two decimals, such as `"4210.00"`. Negative is allowed, for credits and refunds that outweigh a month's invoices. A JSON number is refused, so no digits get lost on the way. |
| `currency` | A three-letter code, such as `USD`. Revenue is kept per currency and never converted, and only USD is compared with cost. |

A customer, month and currency can appear once in a request.

### The newer `as_of` wins

If TensorCost already holds a figure for that customer, month and currency with a later `as_of`, your row is skipped and listed in the response. An equal `as_of` writes, so the last request in wins a tie. This is what makes retries and late jobs safe: an old batch that arrives after a newer one can't roll the figure back.

A CSV upload replaces whatever is held for the customers and months it names, whatever their `as_of`.

## Sign the request

Make a signing secret under **Connect › API keys**, in **Revenue signing secrets**, with **New signing secret**. You get two values:

* A **key id**, `tcr_` and 24 hex characters. It isn't secret and goes in a header.
* A **secret**, `tcrs_` and 64 hex characters. It is shown once. Keep it in your job's secret store.

Send three headers with each request:

| Header | Value |
| - | - |
| `X-TensorCost-Key-Id` | The key id. |
| `X-TensorCost-Timestamp` | The time you signed, in Unix seconds, digits only. Refused when it is more than 5 minutes either side of TensorCost's clock. |
| `X-TensorCost-Signature` | The hex HMAC-SHA256 of `${timestamp}.${rawBody}`, keyed with the whole secret string, `tcrs_` included. 64 characters. |

`rawBody` is the exact bytes you send. Sign the string you put on the wire, not an object you serialise a second time, because a difference in spacing or key order is a different signature. Send it with `Content-Type: application/json`, to the path with no trailing slash.

```bash curl theme={null}
# TC_KEY_ID looks like tcr_0123456789abcdef01234567
# TC_SECRET looks like tcrs_ followed by 64 hex characters
TS=$(date +%s)
AS_OF=$(date -u +%Y-%m-%dT%H:%M:%SZ)
BODY=$(printf '{"request_id":"revenue-%s","as_of":"%s","rows":[{"customer":"acme","month":"2026-11","revenue":"4210.00","currency":"USD"}]}' "$TS" "$AS_OF")
SIG=$(printf '%s.%s' "$TS" "$BODY" | openssl dgst -sha256 -hmac "$TC_SECRET" -hex | sed 's/^.* //')

curl -sS -X POST https://api.tensorcost.com/v1/api/cost/revenue \
  -H "Content-Type: application/json" \
  -H "X-TensorCost-Key-Id: $TC_KEY_ID" \
  -H "X-TensorCost-Timestamp: $TS" \
  -H "X-TensorCost-Signature: $SIG" \
  --data-binary "$BODY"
```

```js Node.js theme={null}
import { createHmac, randomUUID } from "node:crypto";

const keyId = process.env.TC_KEY_ID;
const secret = process.env.TC_SECRET;

const body = JSON.stringify({
  request_id: `revenue-${randomUUID()}`,
  as_of: new Date().toISOString(),
  rows: [{ customer: "acme", month: "2026-11", revenue: "4210.00", currency: "USD" }],
});

const timestamp = String(Math.floor(Date.now() / 1000));
const signature = createHmac("sha256", secret).update(`${timestamp}.${body}`).digest("hex");

const res = await fetch("https://api.tensorcost.com/v1/api/cost/revenue", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "X-TensorCost-Key-Id": keyId,
    "X-TensorCost-Timestamp": timestamp,
    "X-TensorCost-Signature": signature,
  },
  body, // the same string that was signed
});
console.log(res.status, await res.json());
```

```python Python theme={null}
import hashlib, hmac, json, os, time, uuid
from datetime import datetime, timezone
import requests

key_id = os.environ["TC_KEY_ID"]
secret = os.environ["TC_SECRET"]

body = json.dumps({
    "request_id": f"revenue-{uuid.uuid4()}",
    "as_of": datetime.now(timezone.utc).isoformat(timespec="seconds"),
    "rows": [{"customer": "acme", "month": "2026-11", "revenue": "4210.00", "currency": "USD"}],
})

timestamp = str(int(time.time()))
signature = hmac.new(secret.encode(), f"{timestamp}.{body}".encode(), hashlib.sha256).hexdigest()

res = requests.post(
    "https://api.tensorcost.com/v1/api/cost/revenue",
    headers={
        "Content-Type": "application/json",
        "X-TensorCost-Key-Id": key_id,
        "X-TensorCost-Timestamp": timestamp,
        "X-TensorCost-Signature": signature,
    },
    data=body.encode(),  # the same bytes that were signed
)
print(res.status_code, res.json())
```

A good request answers 200:

```json theme={null}
{
  "request_id": "revenue-2026-11-15T10-00",
  "as_of": "2026-11-15T10:00:00.000Z",
  "received": 2,
  "written": 1,
  "skipped": [
    { "index": 1, "customer": "globex", "month": "2026-11", "currency": "USD", "held_as_of": "2026-11-15T10:30:00.000Z" }
  ],
  "warnings": []
}
```

`index` is the row's position in `rows`, counting from 0. `warnings` carries the same index and a message, for example for an email-shaped customer id.

## Retrying safely

The `request_id` is how TensorCost knows two requests are the same one. Ids belong to your workspace and cover pushes and deletes together.

* A request that **succeeded** keeps its id for 24 hours. Sending it again answers `409` with `code: "REQUEST_ID_SEEN"`. It is already written, so stop. New figures need a new id.
* A request that is **still being written**, or whose earlier attempt died part-way, answers `409` with `code: "REQUEST_ID_PENDING"` and a `Retry-After` header in seconds, at most 300. Send the same request again after that long.
* A request that **failed** (a 400 or a 5xx) can be sent again with the same id.

```json theme={null}
{
  "statusCode": 409,
  "code": "REQUEST_ID_PENDING",
  "message": "This request id is still being written, or an earlier attempt didn't finish. Retry after the Retry-After seconds."
}
```

When you retry, sign again with a fresh timestamp. The old one may have aged out of the 5-minute window.

## When a request is refused

A batch is taken whole or not at all. One bad row refuses the request and nothing is written. The 400 lists every bad row by its index, so you can fix them in one go:

```json theme={null}
{
  "statusCode": 400,
  "error": "Bad Request",
  "message": "Nothing was written: 2 rows have a problem.",
  "errors": [
    { "index": 0, "message": "customer has leading or trailing spaces; calls are recorded without them, so it would never match" },
    { "index": 3, "message": "revenue \"12.345\" has more than 2 decimal places" }
  ]
}
```

An `index` of `null` is about the body itself, for example a missing `request_id`.

| Status | Meaning |
| - | - |
| 400 | The body has a problem. Nothing was written. |
| 401 | A header is missing or malformed, the timestamp is more than 5 minutes off, the signature doesn't match, or the key is unknown or revoked. |
| 403 | The workspace isn't on Scale or Enterprise. |
| 409 | `REQUEST_ID_SEEN` or `REQUEST_ID_PENDING`, as above. |
| 413 | The body is over 1 MiB. Split the batch. |
| 415 | The body wasn't sent as JSON. |
| 429 | A rate limit. Wait the `Retry-After` seconds. |
| 503 | TensorCost couldn't check the request just now. Nothing was written. Try again shortly. |

## Limits

* 1,000 rows and 1 MiB a request.
* 60 requests a minute for a workspace.
* Before the signature is checked, 120 requests a minute from one address naming one key id.
* After 30 requests in a minute that name a key and fail the signature check, that key's requests get 429 until the minute ends, even well-signed ones. A key id isn't secret, so someone else can cause this. If it keeps happening, make a new signing secret, switch to it and revoke the old one.

## Delete revenue

`DELETE /v1/api/cost/revenue` takes a signed JSON body, signed exactly as a push is, with the same ids, retries, window and limits. Send one of two shapes:

```json theme={null}
{ "request_id": "remove-acme-2026-11", "customer": "acme", "month": "2026-11" }
```

```json theme={null}
{ "request_id": "remove-everything-0001", "all": true }
```

The first removes that customer's month in every currency. The second removes all of the workspace's revenue, whether it came by push or CSV. Sending both shapes, or neither, is a 400 and nothing is deleted. `all` must be exactly `true`. The answer is `{ "request_id": "...", "deleted": 3 }`, where 0 means that month held nothing.

## Rotating the signing secret

A workspace can have two active secrets at once, which is what makes rotation painless.

1. Make a new signing secret. While two are active, a third is refused until you revoke one.
2. Put the new key id and secret in your job and let it run once.
3. Revoke the old one. A job still signing with it is refused from its next push.

Secrets belong to the workspace, not to the admin who made them. When that admin leaves, the secret keeps working and the other admins get an alert to rotate. Nothing revokes it for you. Each secret shows when it was last used, which tells you whether anything still signs with the old one before you revoke it.

## What it feeds

Revenue shows up under **Spend › Customers** as revenue and margin, and sets each customer's limit in a [margin floor](https://docs.tensorcost.com/spend/budgets.md#every-customer-keeps-a-margin). The other direction is [Pull customer usage](https://docs.tensorcost.com/developers/usage-pull.md).


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