---
title: "REST API: payments and goals"
description: "The DataPocket Payment API and server-side goals: authentication, every field, idempotency, currencies, how payments find their visitor, responses and errors."
url: "https://datapocket.vo1d.studio/reference/rest-api"
lang: "en"
updated: "2026-11-13"
---

# REST API: payments and goals

Send payments with `POST /api/v1/payments` and goals with `POST /api/v1/goals`, authenticated with a site API key as `Authorization: Bearer dpk_…`. A payment needs `amount`, `currency` and `transaction_id`, which is the idempotency key: sending the same id again updates that payment instead of adding one. `visitor_id` is optional but is what attributes the sale.

## Authentication

Create a key in your site's Settings → API keys. Site keys (`dpk_…`) belong to one site. Account tokens (`dpt_…`) need `site_id` (your site's public id) in the body or query. Writing needs a key with the write scope.

```bash
curl -X POST https://datapocket.vo1d.studio/api/v1/payments \
  -H "Authorization: Bearer dpk_…" \
  -H "Content-Type: application/json" \
  -d '{"amount": 29.99, "currency": "USD", "transaction_id": "order_123", "visitor_id": "<dp_vid cookie>"}'
```

## POST /api/v1/payments

| Field | Required | Notes |
|---|---|---|
| `amount` | Yes | Number in major units (`29.99`, or `500` for 500 JPY), 0 to 1,000,000,000. `0` records a trial. |
| `currency` | Yes | Three-letter ISO 4217 code, any case. |
| `transaction_id` | Yes | 1 to 255 characters, unique per site. The idempotency key (below). |
| `visitor_id` | No | The buyer's `dp_vid` cookie (or `window.datapocket.visitorId` in cookieless mode). 8 to 64 characters of `A-Z a-z 0-9 _ -`. |
| `session_id` | No | The `dp_sid` cookie, to credit the session as well. |
| `customer_id` | No | Your id for the customer. Later payments with the same id inherit the visitor of their first payment. |
| `email`, `name` | No | The buyer. An email also matches a visitor you linked with `identify`. |
| `renewal` | No | `true` for a subscription renewal. |
| `refunded` | No | `true` marks the payment refunded; it stops counting as revenue. |
| `timestamp` | No | ISO 8601 with offset (`2026-11-13T09:30:00Z`). Defaults to now. |
| `site_id` | With account tokens | Your site's public id. |

Response: `{"status": "success", "data": {"transaction_id": "order_123", "attributed": true}}`. `attributed` says whether a visitor was found.

Remove a payment with `DELETE /api/v1/payments?transaction_id=order_123`.

## Idempotency

- `transaction_id` identifies the payment within your site, for as long as the payment exists: there is no expiry window.
- Sending the same id again updates the stored payment (amount, `refunded`, `renewal`, customer fields) instead of adding a second one, so retries are safe. The purchase date stays the first one unless you mark a refund.
- The first visitor to be attributed stays: a later request with a different `visitor_id` doesn't move the sale. The one exception is a credit that came from a browser `datapocket("payment")` call, which your server's `visitor_id` replaces.

## Currencies

Amounts are converted to your site's currency with the European Central Bank's reference rates for the payment's date, and the original amount and currency are kept as well. ECB publishes rates for about 30 major currencies; a payment in a currency it doesn't cover is kept with its original amount but left out of revenue until a rate is available, so send those in your site's currency. Zero-decimal currencies (JPY, KRW, CLP, VND…) are handled: send the amount as you'd show it.

## How a payment finds its visitor

1. `visitor_id` in the request.
2. An earlier payment with the same `customer_id`.
3. `datapocket("payment", { transaction_id })` called in the buyer's browser with the same `transaction_id`, up to a day before the request or an hour after. Browser calls can't be verified, so this counts only when a single visitor sent it for that transaction: if a second, different visitor sends one within the window, the credit is withdrawn and the payment stays unattributed. A `visitor_id` your server sends later replaces a credit that came from a browser call.
4. An `identify` call with the same `email`.

Without any of these the payment still counts in your revenue, as unattributed. `visitor_id` from your server is the reliable way; the browser call is a fallback for checkouts where your server can't read the buyer's cookie.

## POST /api/v1/goals

Records a goal for a visitor from your server: a conversion that completes in a webhook, or on a page where the script is excluded. The goal takes the location, device and attribution of the visitor's latest pageview, so the visitor needs at least one tracked visit.

```bash
curl -X POST https://datapocket.vo1d.studio/api/v1/goals \
  -H "Authorization: Bearer dpk_…" \
  -H "Content-Type: application/json" \
  -d '{"visitor_id": "<dp_vid cookie>", "name": "ticket_purchased", "metadata": {"event": "spring-gala", "seats": 2}}'
```

| Field | Required | Notes |
|---|---|---|
| `visitor_id` | Yes | As for payments. |
| `name` | Yes | 1 to 64 characters; normalized like browser goals. `payment` and `identify` are reserved. |
| `metadata` | No | Up to 10 properties; keys `[a-z0-9_-]{1,64}`, values strings, numbers or booleans, stored as text up to 255 characters. |
| `site_id` | With account tokens | Your site's public id. |

Response: `{"status": "success", "data": {"name": "ticket_purchased"}}`. A visitor with no tracked pageview gets 404.

## Errors and limits

Errors share one shape: `{"status": "error", "error": {"code": 400, "message": "currency: Invalid"}}`. Validation messages name each failing field.

| Status | When |
|---|---|
| 400 | The body isn't JSON, a field is missing or invalid, a reserved goal name, or `site_id` missing with an account token. |
| 401 | Missing or invalid API key. |
| 403 | The key is read-only. |
| 404 | No site for this key, no payment with that `transaction_id` (DELETE), or no tracked visit for the goal's visitor. |
| 429 | More than 3 requests per second per key on average (bursts up to 120). Retry after a short wait. |

## Questions

### Is visitor_id required?

No. Without it the payment counts as unattributed revenue unless DataPocket can find the visitor another way (customer id, a browser hint with the same transaction id, or an identified email).

### Is there an SDK?

Not on the npm registry yet. The API is plain JSON over HTTPS, so any HTTP client works.

## Related

- [Read API: stats, visitors, funnels and insights](https://datapocket.vo1d.studio/reference/read-api)
- [Tracking script reference](https://datapocket.vo1d.studio/reference/tracking-script)
- [Track Stripe revenue by traffic source](https://datapocket.vo1d.studio/guides/stripe-revenue-attribution)
- [All reference](https://datapocket.vo1d.studio/reference)
