> For the complete documentation index, see [llms.txt](https://docs.humblytics.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.humblytics.com/how-to-track-purchase-events/stripe/stripe-api.md).

# Stripe API

For custom checkouts that call the Stripe API: pass the visitor's Humblytics view ID into Stripe metadata.

{% hint style="info" %}
[Connect Stripe](/how-to-track-purchase-events/stripe.md) first. This guide is for developers who control the code that creates Stripe payments.
{% endhint %}

## How it works

The tracking script gives every visit an ID. Send that ID to Stripe as `humblytics_view_id` metadata, and Humblytics matches the payment to that visit. You don't need any webhooks.

## Step 1: Read the view ID in the browser

```javascript
const viewId = window.Humblytics.viewId;

await fetch("/api/create-checkout", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ priceId, humblytics_view_id: viewId }),
});
```

## Step 2: Add it to Stripe metadata on your server

Add `humblytics_view_id` to whichever Stripe object your flow creates. Humblytics checks **Checkout Sessions**, **Payment Intents**, **Invoices**, **Subscriptions**, and **Customers**, and uses the first one it finds.

```javascript
// Stripe Checkout
const session = await stripe.checkout.sessions.create({
  line_items,
  mode: "payment",
  metadata: { humblytics_view_id },
});

// One-time payment
const paymentIntent = await stripe.paymentIntents.create({
  amount,
  currency,
  metadata: { humblytics_view_id },
});

// Subscription
const subscription = await stripe.subscriptions.create({
  customer: customerId,
  items: [{ price: priceId }],
  metadata: { humblytics_view_id },
});
```

{% hint style="info" %}
**Subscriptions and repeat buyers:** put the ID on the **Subscription** or **Customer**. Humblytics then applies it to future invoices and renewals from that customer too.
{% endhint %}

## Troubleshooting

* **Revenue, but no attribution:** in Stripe, open the payment and check its metadata for `humblytics_view_id`. If `viewId` was `undefined`, the tracking script hadn't loaded on the page yet.
* **Still stuck?** Email **<support@humblytics.com>**.
