> For the complete documentation index, see [llms.txt](https://docs.tiun.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tiun.io/guides/one-time-purchases/verify-server-side.md).

# Verify repeatable purchases server-side

Verifying a repeatable purchase on the backend is **identity verification plus a per-reference access check**. If your server returns content that should only go to customers who bought a specific item — an article, a booking, a report — this guide is what you want.

Steps 1 and 2 are the same as [server-side authentication verification](https://docs.tiun.io/guides/authentication/verify-server-side.md). The difference is step 3: instead of looking at `productAccess`, you ask tiun about the **product and reference ID** together.

{% hint style="info" %}
This page is for **repeatable** one-time products. For regular one-time products and subscriptions, a `productAccess` check is enough — see [server-side subscription verification](https://docs.tiun.io/guides/subscriptions/verify-server-side.md).
{% endhint %}

***

## How it works

1. Your frontend asks tiun for a signed verification token and sends it to your backend with the request for an item.
2. Your backend exchanges the token with the [tiun UserVerification API](https://docs.tiun.io/api/user-verification.md) and gets the `userId`.
3. Your backend asks the product access API whether that user has access to the product **for that reference ID**.
4. If `hasAccess` is `true`, you serve the item.

***

## Setup: API key

Create an API key in the dashboard under **API & Webhook** → **API Keys**, then store it in your backend environment variables. Same key as the one used for [server-side authentication verification](https://docs.tiun.io/guides/authentication/verify-server-side.md).

***

## 1. Get the verification token

Identical to the auth verification flow — call `tiun.getUserVerificationToken()` on the frontend and send the result to your backend as a Bearer token. Get a fresh token for every request; parallel requests can each fetch their own. See [getting the verification token](https://docs.tiun.io/guides/authentication/verify-server-side.md#1-get-the-verification-token) for the snippet.

***

## 2. Verify the token on your server

Identical to the auth verification flow — POST the token to the UserVerification API and check `isAuthenticated`. See [verifying the token on your server](https://docs.tiun.io/guides/authentication/verify-server-side.md#2-verify-the-token-on-your-server). Keep `user.userId` from the response for the next step.

***

## 3. Check access for the reference ID

**Endpoint:**

`GET /live_api/s2s/v1/users/{userId}/product_access/{productId}?referenceId={referenceId}`

**Base URLs:**

| Environment | URL                             |
| ----------- | ------------------------------- |
| Live        | `https://api.tiun.live`         |
| Sandbox     | `https://api-sandbox.tiun.live` |

Use the base URL and API key from the **same environment** as your frontend.

**Header:** `X-TIUN-API-KEY: <your-api-key>`

| Parameter     | In    | Description                                  |
| ------------- | ----- | -------------------------------------------- |
| `userId`      | path  | The tiun user ID from the verified response. |
| `productId`   | path  | The repeatable product's ID.                 |
| `referenceId` | query | The reference ID the purchase was made with. |

**Response codes:**

| Status | Meaning                                |
| ------ | -------------------------------------- |
| `200`  | Access result returned — read the body |
| `400`  | The request is invalid                 |
| `401`  | API key is invalid                     |
| `404`  | The user or product does not exist     |

A `200` response tells you whether the user has access for that reference, and if so, since when and until when:

```json
{
  "hasAccess": true,
  "access": {
    "productId": "p-live-article",
    "referenceId": "article-1042",
    "type": "OneTime",
    "activatedAt": "2026-11-02T09:21:58Z",
    "expiresAt": null
  }
}
```

`expiresAt` is `null` unless the product has **limited access**. Without access, `hasAccess` is `false` and there's no `access` object.

***

## Full round-trip

**Frontend** — request the item with a verification token. See [the repeatable purchases guide](https://docs.tiun.io/guides/one-time-purchases/repeatable-one-time-purchases.md#5-check-access-and-gate-content) for the full frontend side.

**Backend** — verify the user, then check the purchase for the requested article:

```javascript
import express from 'express';

const app = express();

const BASE_URL = process.env.TIUN_API_BASE || 'https://api-sandbox.tiun.live';
const API_KEY = process.env.TIUN_API_KEY;
const ARTICLE_PRODUCT = 'p-live-article';

app.get('/api/articles/:articleId', async (req, res) => {
  const auth = req.headers.authorization;
  const token = auth?.startsWith('Bearer ') ? auth.slice(7) : null;

  if (!token) {
    // missing token → reject as unauthenticated
    return res.sendStatus(401);
  }

  const verifyResponse = await fetch(
    `${BASE_URL}/live_api/s2s/v2/users/verification`,
    {
      method: 'POST',
      headers: {
        'X-TIUN-API-KEY': API_KEY,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ userVerificationToken: token }),
    },
  );

  if (verifyResponse.status !== 200) {
    // API key invalid or upstream error → fail closed
    return res.sendStatus(502);
  }

  const { isAuthenticated, user } = await verifyResponse.json();

  if (!isAuthenticated) {
    // not signed in → reject
    return res.sendStatus(401);
  }

  const accessUrl = new URL(
    `/live_api/s2s/v1/users/${user.userId}/product_access/${ARTICLE_PRODUCT}`,
    BASE_URL,
  );
  accessUrl.searchParams.set('referenceId', req.params.articleId);

  const accessResponse = await fetch(accessUrl, {
    headers: { 'X-TIUN-API-KEY': API_KEY },
  });

  if (accessResponse.status !== 200) {
    // unknown user or product, or upstream error → fail closed
    return res.sendStatus(403);
  }

  const { hasAccess } = await accessResponse.json();

  if (!hasAccess) {
    // signed in but hasn't bought this article → reject (prompt purchase)
    return res.sendStatus(403);
  }

  // user bought this article → serve it
  res.json(await loadArticle(req.params.articleId));
});
```

***

## All purchases of a user

To list everything a user has access to — every product and reference ID pair — call the same API without a product:

`GET /live_api/s2s/v1/users/{userId}/product_access`

It takes the same header and returns the same `access` objects in a `data` array:

```json
{
  "data": [
    {
      "productId": "p-live-article",
      "referenceId": "article-1042",
      "type": "OneTime",
      "activatedAt": "2026-11-02T09:21:58Z",
      "expiresAt": null
    }
  ]
}
```

Use it for a "My purchases" page, or to sync a user's purchases into your own database.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tiun.io/guides/one-time-purchases/verify-server-side.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
