> For the complete documentation index, see [llms.txt](https://docs.promoteboost.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.promoteboost.com/tracking-setup/stripe-api.md). Agents can also search and read this documentation through its [MCP server](https://docs.promoteboost.com/~gitbook/mcp).

# Stripe API

For applications using the Stripe API directly (not Checkout).

***

## When to Use This

Use this method if you:

* Create charges or payment intents via API
* Use Stripe Billing for subscriptions
* Have a custom payment flow
* Don't use Stripe Checkout

***

## Step 1: Install Tracking Script

Add to your frontend:

```html
<script src="https://api.promoteboost.com/tracker.js" data-org="YOUR_ORG_ID"></script>
```

***

## Step 2: Get Referral ID

On your frontend, retrieve the referral:

```javascript
const referralId = window.promoteboost_referral || null;
```

Send this to your backend with the payment request.

***

## Step 3: Add Metadata to Stripe

### For Payment Intents

```javascript
const paymentIntent = await stripe.paymentIntents.create({
  amount: 2000,
  currency: 'usd',
  customer: customerId,
  metadata: {
    promoteboost_referral: referralId,
  },
});
```

### For Subscriptions

```javascript
const subscription = await stripe.subscriptions.create({
  customer: customerId,
  items: [{ price: 'price_xxx' }],
  metadata: {
    promoteboost_referral: referralId,
  },
});
```

### For Invoices

```javascript
const invoice = await stripe.invoices.create({
  customer: customerId,
  metadata: {
    promoteboost_referral: referralId,
  },
});
```

***

## Step 4: Customer Metadata (Optional)

For recurring attribution, also add to the customer:

```javascript
const customer = await stripe.customers.create({
  email: 'customer@example.com',
  metadata: {
    promoteboost_referral: referralId,
  },
});
```

This ensures future purchases from this customer are attributed to the affiliate.

***

## Complete Example

### Frontend

```javascript
// Get referral from PromoteBoost tracker
const referralId = window.promoteboost_referral;

// Send to your backend
const response = await fetch('/api/subscribe', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    email: 'customer@example.com',
    priceId: 'price_xxx',
    referralId: referralId,
  }),
});
```

### Backend (Node.js)

```javascript
app.post('/api/subscribe', async (req, res) => {
  const { email, priceId, referralId } = req.body;

  // Create customer with referral metadata
  const customer = await stripe.customers.create({
    email: email,
    metadata: {
      promoteboost_referral: referralId || '',
    },
  });

  // Create subscription with referral metadata
  const subscription = await stripe.subscriptions.create({
    customer: customer.id,
    items: [{ price: priceId }],
    metadata: {
      promoteboost_referral: referralId || '',
    },
  });

  res.json({ subscriptionId: subscription.id });
});
```

***

## Webhooks

PromoteBoost automatically listens for these Stripe events:

| Event                           | Action                              |
| ------------------------------- | ----------------------------------- |
| `invoice.paid`                  | Records sale, calculates commission |
| `charge.refunded`               | Cancels commission                  |
| `customer.subscription.deleted` | Ends recurring tracking             |

No additional webhook setup needed — it's handled when you connect Stripe.

***

## Done!

Sales are now tracked via the Stripe API. Test with a real payment in Stripe test mode.
