REST API: payments and goals

Updated

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

FieldRequiredNotes
amountYesNumber in major units (29.99, or 500 for 500 JPY), 0 to 1,000,000,000. 0 records a trial.
currencyYesThree-letter ISO 4217 code, any case.
transaction_idYes1 to 255 characters, unique per site. The idempotency key (below).
visitor_idNoThe buyer's dp_vid cookie (or window.datapocket.visitorId in cookieless mode). 8 to 64 characters of A-Z a-z 0-9 _ -.
session_idNoThe dp_sid cookie, to credit the session as well.
customer_idNoYour id for the customer. Later payments with the same id inherit the visitor of their first payment.
email, nameNoThe buyer. An email also matches a visitor you linked with identify.
renewalNotrue for a subscription renewal.
refundedNotrue marks the payment refunded; it stops counting as revenue.
timestampNoISO 8601 with offset (2026-11-13T09:30:00Z). Defaults to now.
site_idWith account tokensYour 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}}'
FieldRequiredNotes
visitor_idYesAs for payments.
nameYes1 to 64 characters; normalized like browser goals. payment and identify are reserved.
metadataNoUp to 10 properties; keys [a-z0-9_-]{1,64}, values strings, numbers or booleans, stored as text up to 255 characters.
site_idWith account tokensYour 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.

StatusWhen
400The body isn't JSON, a field is missing or invalid, a reserved goal name, or site_id missing with an account token.
401Missing or invalid API key.
403The key is read-only.
404No site for this key, no payment with that transaction_id (DELETE), or no tracked visit for the goal's visitor.
429More 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

REST API: payments and goals · DataPocket