# Sell your first key with the API and a webhook

Create a product, give it keys, put it on sale, and have your server told the moment someone pays. Your store is live from day one, so one live key does every step. Once anyshop has approved your store, you check the whole flow with one real purchase, then refund it.

- 8 steps
- curl, Node or Python
- A public https server for the webhook

API key scopes used: `products:write` (live key), `stock:write` (live key), `webhooks:write` (live key), `checkouts:write` (live key), `orders:write` (live key).

> **One key for every step.** Every anyshop API key is a live key. Make one with the scopes this guide uses, and every step below sends it. The badge on each step says so.

> **See a sale first.** Try the checkout on the demo store at [demo.anyshop.io](https://demo.anyshop.io). It sells a sample license and a sample download without taking any money, so you can see what your buyers get before you build anything.

> **Before anyshop has approved your store**, steps 1 to 6 work as written: you make keys, create and publish products, add stock and add webhook endpoints from day one. Step 7 starts a checkout, which answers `store_not_approved` (409) until anyshop has approved your store.

## Step 1: Make an API key

In the dashboard, open Developers, API keys, and choose New API key. Give it `products:write`, `stock:write`, `webhooks:write`, `checkouts:write` and `orders:write` (it asks for your two-factor code if two-factor is on). The key is shown once; keep it in your server's environment, never in a browser or an app.

.env:

```env
ANYSHOP_LIVE_KEY=as_live_7Fq2...Q7pX
```

## Step 2: Create the product (live key)

A product starts as a draft. Its price is an integer in minor units of your store's currency: 1999 is $19.99. Send an `Idempotency-Key` so a retry after a timeout never makes two products.

curl:

```bash
curl https://api.anyshop.io/v1/products \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY" \
  -H "Idempotency-Key: first-product-1" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Lumen Studio Pro",
    "type": "serial",
    "price": 1999,
    "platform": "Windows and macOS",
    "delivery_instructions": "In Lumen Studio Pro, choose Help, Enter license key, and paste this key."
  }'
```

Node:

```js
const response = await fetch("https://api.anyshop.io/v1/products", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}`,
    "Idempotency-Key": "first-product-1",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    name: "Lumen Studio Pro",
    type: "serial",
    price: 1999,
    platform: "Windows and macOS",
    delivery_instructions: "In Lumen Studio Pro, choose Help, Enter license key, and paste this key.",
  }),
});
const product = await response.json();
```

Python:

```python
import os
import requests

response = requests.post(
    "https://api.anyshop.io/v1/products",
    headers={
        "Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}",
        "Idempotency-Key": "first-product-1",
    },
    json={
        "name": "Lumen Studio Pro",
        "type": "serial",
        "price": 1999,
        "platform": "Windows and macOS",
        "delivery_instructions": "In Lumen Studio Pro, choose Help, Enter license key, and paste this key.",
    },
)
product = response.json()
```

Answer (200 Response, shortened):

```json
{
  "object": "product",
  "id": "prod_01m3rr5kmbeb2s18x1kpntjxqb",
  "slug": "lumen-studio-pro",
  "status": "draft",
  "currency": "USD",
  "variants": [
    { "id": "var_01m3rr5kmd6h0q2t8w4y9zb7ce", "name": "Standard", "price": 1999 }
  ],
  "published_at": null
}
```

## Step 3: Add keys to its stock (live key)

These are the keys your buyers get, so add real ones. Duplicates within the upload and keys already in stock are left out and counted, never added twice.

curl:

```bash
curl \
  https://api.anyshop.io/v1/products/prod_01m3rr5kmbeb2s18x1kpntjxqb/stock_items \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "keys": ["LUMEN-K2LM-3F9Q", "LUMEN-P8WN-4H7R", "LUMEN-P8WN-4H7R"] }'
```

Node:

```js
const product = "prod_01m3rr5kmbeb2s18x1kpntjxqb";
const response = await fetch(`https://api.anyshop.io/v1/products/${product}/stock_items`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ keys: ["LUMEN-K2LM-3F9Q", "LUMEN-P8WN-4H7R", "LUMEN-P8WN-4H7R"] }),
});
const upload = await response.json();
```

Python:

```python
import os
import requests

product = "prod_01m3rr5kmbeb2s18x1kpntjxqb"
response = requests.post(
    f"https://api.anyshop.io/v1/products/{product}/stock_items",
    headers={"Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}"},
    json={"keys": ["LUMEN-K2LM-3F9Q", "LUMEN-P8WN-4H7R", "LUMEN-P8WN-4H7R"]},
)
upload = response.json()
```

Answer (200 Response):

```json
{
  "object": "stock_upload",
  "product": "prod_01m3rr5kmbeb2s18x1kpntjxqb",
  "livemode": true,
  "submitted": 3,
  "added": 2,
  "repeated_in_upload": 1,
  "already_in_stock": 0
}
```

## Step 4: Put it on sale (live key)

Publishing makes the product active. A serial key product can go on sale with no stock; it shows as sold out until keys arrive.

curl:

```bash
curl -X POST \
  https://api.anyshop.io/v1/products/prod_01m3rr5kmbeb2s18x1kpntjxqb/publish \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY"
```

Node:

```js
const product = "prod_01m3rr5kmbeb2s18x1kpntjxqb";
await fetch(`https://api.anyshop.io/v1/products/${product}/publish`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}` },
});
```

Python:

```python
import os
import requests

product = "prod_01m3rr5kmbeb2s18x1kpntjxqb"
requests.post(
    f"https://api.anyshop.io/v1/products/{product}/publish",
    headers={"Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}"},
)
```

## Step 5: Tell anyshop where to send events (live key)

Add a webhook endpoint for `order.paid` and `order.refunded`. It must be a public https address; anyshop never follows redirects or calls private networks. The answer holds the signing secret, shown this once.

curl:

```bash
curl https://api.anyshop.io/v1/webhook_endpoints \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "url": "https://example.com/anyshop",
        "events": ["order.paid", "order.refunded"] }'
```

Node:

```js
const response = await fetch("https://api.anyshop.io/v1/webhook_endpoints", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    url: "https://example.com/anyshop",
    events: ["order.paid", "order.refunded"],
  }),
});
const endpoint = await response.json(); // endpoint.secret.value, shown once
```

Python:

```python
import os
import requests

response = requests.post(
    "https://api.anyshop.io/v1/webhook_endpoints",
    headers={"Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}"},
    json={"url": "https://example.com/anyshop", "events": ["order.paid", "order.refunded"]},
)
endpoint = response.json()  # endpoint["secret"]["value"], shown once
```

Answer (200 Response, shortened):

```json
{
  "object": "webhook_endpoint",
  "id": "whe_01m3rs9d2f4k6m8p0r2t4v6x8z",
  "livemode": true,
  "signing": "hmac",
  "status": "enabled",
  "secret": {
    "object": "webhook_secret",
    "algorithm": "hmac",
    "value": "whsec_Q3s9mV1...kP4="
  }
}
```

## Step 6: Verify, then act

Events are signed with Standard Webhooks, so any Standard Webhooks library checks them. Verify against the raw body, answer with a 2xx quickly, and do the slow work after. The same event can arrive twice: use its `webhook-id` header to do the work once.

Node (server.js):

```js
import express from "express";
import { Webhook } from "standardwebhooks";

const webhook = new Webhook(process.env.ANYSHOP_WEBHOOK_SECRET); // whsec_...
const app = express();

app.post("/anyshop", express.raw({ type: "application/json" }), (req, res) => {
  let event;
  try {
    event = webhook.verify(req.body, req.headers);
  } catch {
    return res.sendStatus(400);
  }
  if (event.type === "order.paid") {
    // yours: do it once per req.headers["webhook-id"]
    grantAccess(event.data.buyer.email, event.data.items);
  }
  res.sendStatus(204);
});

app.listen(3000);
```

Python (server.py):

```python
import os
from flask import Flask, request
from standardwebhooks.webhooks import Webhook

webhook = Webhook(os.environ["ANYSHOP_WEBHOOK_SECRET"])  # whsec_...
app = Flask(__name__)

@app.post("/anyshop")
def anyshop():
    try:
        event = webhook.verify(request.get_data(), dict(request.headers))
    except Exception:
        return "", 400
    if event["type"] == "order.paid":
        # yours: do it once per request.headers["webhook-id"]
        grant_access(event["data"]["buyer"]["email"], event["data"]["items"])
    return "", 204
```

## Step 7: Buy it once, for real (live key)

Once anyshop has approved your store, and Stripe or PayPal is connected in Settings, Payments, start a checkout for the product's variant (`variants[0].id` in the product you created) and your own email. The return addresses may be your store's own address, so nothing else needs setting up.

curl:

```bash
curl https://api.anyshop.io/v1/checkout_sessions \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "variant": "var_01m3rr5kmd6h0q2t8w4y9zb7ce",
    "buyer_email": "you@example.com",
    "success_url": "https://your-store.anyshop.io/",
    "cancel_url": "https://your-store.anyshop.io/"
  }'
```

Node:

```js
const response = await fetch("https://api.anyshop.io/v1/checkout_sessions", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    variant: "var_01m3rr5kmd6h0q2t8w4y9zb7ce",
    buyer_email: "you@example.com",
    success_url: "https://your-store.anyshop.io/",
    cancel_url: "https://your-store.anyshop.io/",
  }),
});
const session = await response.json();
console.log(session.url);
```

Python:

```python
import os
import requests

response = requests.post(
    "https://api.anyshop.io/v1/checkout_sessions",
    headers={"Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}"},
    json={
        "variant": "var_01m3rr5kmd6h0q2t8w4y9zb7ce",
        "buyer_email": "you@example.com",
        "success_url": "https://your-store.anyshop.io/",
        "cancel_url": "https://your-store.anyshop.io/",
    },
)
print(response.json()["url"])
```

Open the session's `url` and pay with your own card or PayPal account. The key is delivered on the spot, the order is listed under Orders, and your server gets this:

POST /anyshop:

```json
{
  "type": "order.paid",
  "timestamp": "2026-10-02T14:32:05.112Z",
  "data": {
    "object": "order",
    "id": "ord_01m3rt5x2c7q9f0a4d6k8h1n3p",
    "number": 1001,
    "livemode": true,
    "status": "paid",
    "currency": "USD",
    "total": 1999,
    "buyer": { "email": "you@example.com" },
    "paid_with": "stripe",
    "withdrawal_consent": null,
    "items": [
      {
        "product": { "id": "prod_01m3rr5kmbeb2s18x1kpntjxqb", "name": "Lumen Studio Pro" },
        "quantity": 1,
        "unit_amount": 1999
      }
    ]
  }
}
```

> **Server was down?** Nothing is lost. A delivery is tried 10 times over about 3 days, and Developers, Webhooks replays whatever your endpoint missed.

## Step 8: Refund it (live key)

Refund the order from its page in the dashboard, or with `POST /v1/orders/{id}/refund` and the order's ID from `order.paid`. With no `amount` it refunds the whole order, and `revoke_delivered: true` also takes back the key you were sent. Send an `Idempotency-Key` so a retry never refunds twice.

curl:

```bash
curl \
  https://api.anyshop.io/v1/orders/ord_01m3rt5x2c7q9f0a4d6k8h1n3p/refund \
  -H "Authorization: Bearer $ANYSHOP_LIVE_KEY" \
  -H "Idempotency-Key: first-refund-1" \
  -H "Content-Type: application/json" \
  -d '{ "revoke_delivered": true }'
```

Node:

```js
const order = "ord_01m3rt5x2c7q9f0a4d6k8h1n3p";
const response = await fetch(`https://api.anyshop.io/v1/orders/${order}/refund`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.ANYSHOP_LIVE_KEY}`,
    "Idempotency-Key": "first-refund-1",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ revoke_delivered: true }),
});
const refunded = await response.json();
```

Python:

```python
import os
import requests

order = "ord_01m3rt5x2c7q9f0a4d6k8h1n3p"
response = requests.post(
    f"https://api.anyshop.io/v1/orders/{order}/refund",
    headers={
        "Authorization": f"Bearer {os.environ['ANYSHOP_LIVE_KEY']}",
        "Idempotency-Key": "first-refund-1",
    },
    json={"revoke_delivered": True},
)
refunded = response.json()
```

Answer (200 Response, shortened):

```json
{
  "object": "order",
  "id": "ord_01m3rt5x2c7q9f0a4d6k8h1n3p",
  "number": 1001,
  "livemode": true,
  "status": "refunded",
  "total": 1999,
  "refunded": 1999
}
```

The money goes back the way you paid, and your server gets `order.refunded`. Your payment provider may keep its own fee for the payment.

## You are live

There is nothing to switch over. The product, its stock and the endpoint you made are the ones your buyers use. Share the product's link or add a buy button, and every sale reaches your server as order.paid.

[Next: react to every event](https://docs.anyshop.io/guides/webhooks.md)
