Source: https://datafa.st/docs/api-create-payment
Markdown source: https://datafa.st/docs/api-create-payment.md
Description: Track revenue from any payment provider using custom webhook endpoint.

# Payment

> If you're using Stripe, LemonSqueezy, or Polar, you don't need to use this endpoint. We automatically track payments if you have [connected your payment provider](/docs/connect-payment-provider).

# Create a payment

`POST https://datafa.st/api/v1/payments`

Track payments from any provider and attribute revenue to your traffic sources. Requires [Bearer Token](/docs/api-introduction) authentication.

## Request body

Send a JSON object with the following fields:

#### Required fields

- **`amount`** (number): Payment amount. Examples: `29.99` for $29.99, `0` for free trials
- **`currency`** (string): Currency code like `"USD"`, `"EUR"`, `"GBP"`
- **`transaction_id`** (string): Unique transaction ID from your payment provider

#### Optional fields

- **`datafast_visitor_id`** (string): DataFast visitor ID from browser cookies. **⚠️ It's highly recommended to include this field for revenue attribution.**
- **`email`** (string): Customer email
- **`name`** (string): Customer name
- **`customer_id`** (string): Customer ID from your payment provider
- **`renewal`** (boolean): Set to true if its a recurring payment. false by default
- **`refunded`** (boolean): Set to true if its a refunded payment. false by default
- **`timestamp`** (string): Payment timestamp (defaults to now)

## Response

- **Success (200 OK):** Returns confirmation message and transaction ID
- **Errors:** See [API Introduction](/docs/api-introduction)

> Here's an [example](/docs/payments-api) of how to add payment data and attribute revenue using this API endpoint.

## Code examples

### Example request (Node.js/Express)

```javascript
// Send payment data to DataFast's API
const handler = async (req, res) => {
  const datafast_visitor_id = req.cookies.datafast_visitor_id;

  try {
    const response = await fetch("https://datafa.st/api/v1/payments", {
      method: "POST",
      headers: {
        Authorization: `Bearer ${DATAFAST_API_KEY}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        amount: 29.99,
        currency: "USD",
        transaction_id: "payment_456",
        datafast_visitor_id: datafast_visitor_id,
      }),
    });

    res.status(200).send("Payment tracked");
  } catch (error) {
    console.error("Error tracking payment:", error);
    res.status(500).send("Failed to track payment");
  }
};
```

### Success responses (200 OK)

```json
{
  "message": "Payment recorded and attributed successfully",
  "transaction_id": "payment_456"
}
```
