# Quickstart

> Make a key, check who you are, list your campaigns and draft a new one. About five minutes, in cURL, Node or Python.

## 1. Make an API key

1. Sign in to yapfactory.com with your brand account.
2. Open **Settings → AI agents → Make an API key**, turn on **Let it act for me** and make the key.
3. Copy it. It's shown once, and starts with `yf_live_`.

```bash
export YAP_API_KEY="yf_live_..."
```

> Trying things out? Leave **Let it act for me** off for a read-only key, and see [Testing safely](https://yapfactory.com/developers/sandbox.md) for what reaches other people.

## 2. Check who you are

`GET /me` returns your account: brand, creator or both, your country and currency, and what this key is allowed to do.

cURL:

```bash
curl "https://yapfactory.com/api/v1/me" \
  -H "Authorization: Bearer $YAP_API_KEY"
```

Node:

```js
const res = await fetch("https://yapfactory.com/api/v1/me", {
  headers: { Authorization: `Bearer ${process.env.YAP_API_KEY}` },
});
const body = await res.json();
if (!res.ok) throw new Error(`${body.error.code}: ${body.error.message}`);
console.log(body.data);
```

Python:

```python
import os, requests

r = requests.get(
    "https://yapfactory.com/api/v1/me",
    headers={"Authorization": f"Bearer {os.environ['YAP_API_KEY']}"},
)
body = r.json()
if not r.ok:
    raise RuntimeError(f"{body['error']['code']}: {body['error']['message']}")
print(body["data"])
```

```json
{
  "data": {
    "name": "Dana Lee",
    "email": "dana@sleepwell.app",
    "country": "US",
    "currency": "USD",
    "sides": [
      "brand"
    ],
    "brand": {
      "company": "Sleepwell",
      "country": "US",
      "paused": false,
      "home": "https://yapfactory.com/brand"
    },
    "creator": null,
    "canAct": true,
    "gettingStarted": {
      "step": "review_draft",
      "say": "Maya R.'s draft is ready to review.",
      "url": "https://yapfactory.com/deals/3b2f6c1e-7a54-4d0b-9f1a-6e5d4c3b2a19"
    },
    "environment": "live",
    "connection": {
      "app": "API key",
      "kind": "api_key",
      "scopes": [
        "read",
        "write"
      ],
      "manage": "https://yapfactory.com/brand/settings#agents"
    }
  }
}
```

`gettingStarted` is the one next thing worth doing on this account, or `null` when nothing is waiting ([every step](https://yapfactory.com/developers/statuses.md#getting-started)). `connection.scopes` says what this key may do. `canAct` is about the account, not the key: it's false until the email is confirmed.

## 3. List your campaigns

Lists come back as `{ data, nextCursor, hasMore }`. Pass `nextCursor` back as `?cursor=` for the next page, until `hasMore` is false.

cURL:

```bash
curl "https://yapfactory.com/api/v1/campaigns?status=live" \
  -H "Authorization: Bearer $YAP_API_KEY"
```

Node:

```js
const res = await fetch("https://yapfactory.com/api/v1/campaigns?status=live", {
  headers: { Authorization: `Bearer ${process.env.YAP_API_KEY}` },
});
const body = await res.json();
if (!res.ok) throw new Error(`${body.error.code}: ${body.error.message}`);
console.log(body.data);
```

Python:

```python
import os, requests

r = requests.get(
    "https://yapfactory.com/api/v1/campaigns?status=live",
    headers={"Authorization": f"Bearer {os.environ['YAP_API_KEY']}"},
)
body = r.json()
if not r.ok:
    raise RuntimeError(f"{body['error']['code']}: {body['error']['message']}")
print(body["data"])
```

## 4. Draft a campaign

A draft is private: nobody sees it until you publish. Give `productId` from `GET /products`, or `product` to add a new one, as here. Send an `Idempotency-Key`, so a retry never makes two drafts.

cURL:

```bash
curl -X POST "https://yapfactory.com/api/v1/campaigns" \
  -H "Authorization: Bearer $YAP_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "product": {
      "name": "Sleepwell",
      "platform": "ios",
      "url": "https://apps.apple.com/app/id0000000000"
    },
    "title": "Morning routine with Sleepwell",
    "brief": {
      "keyMessage": "Sleepwell helps you fall asleep in 10 minutes without your phone.",
      "hooks": [
        "I used to scroll until 2am."
      ],
      "cta": "Try it free tonight."
    }
  }'
```

Node:

```js
const res = await fetch("https://yapfactory.com/api/v1/campaigns", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.YAP_API_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": crypto.randomUUID(),
  },
  body: JSON.stringify({
    "product": {
      "name": "Sleepwell",
      "platform": "ios",
      "url": "https://apps.apple.com/app/id0000000000"
    },
    "title": "Morning routine with Sleepwell",
    "brief": {
      "keyMessage": "Sleepwell helps you fall asleep in 10 minutes without your phone.",
      "hooks": [
        "I used to scroll until 2am."
      ],
      "cta": "Try it free tonight."
    }
  }),
});
const body = await res.json();
if (!res.ok) throw new Error(`${body.error.code}: ${body.error.message}`);
console.log(body.data.campaignId);
```

Python:

```python
import os, uuid, requests

r = requests.post(
    "https://yapfactory.com/api/v1/campaigns",
    headers={
        "Authorization": f"Bearer {os.environ['YAP_API_KEY']}",
        "Idempotency-Key": str(uuid.uuid4()),
    },
    json={
      "product": {
        "name": "Sleepwell",
        "platform": "ios",
        "url": "https://apps.apple.com/app/id0000000000"
      },
      "title": "Morning routine with Sleepwell",
      "brief": {
        "keyMessage": "Sleepwell helps you fall asleep in 10 minutes without your phone.",
        "hooks": [
          "I used to scroll until 2am."
        ],
        "cta": "Try it free tonight."
      }
    },
)
body = r.json()
if not r.ok:
    raise RuntimeError(f"{body['error']['code']}: {body['error']['message']}")
print(body["data"]["campaignId"])
```

You get `201` with the new draft's id and the page to review it on:

```json
{
  "data": {
    "campaignId": "8f14e45f-ceea-4e7a-9b1d-2c3b4a5d6e7f",
    "status": "draft",
    "review": "https://yapfactory.com/brand/campaigns/8f14e45f-ceea-4e7a-9b1d-2c3b4a5d6e7f/edit",
    "next": "Show the person the draft (get_campaign), then publish_campaign when they're happy."
  }
}
```

When it reads well, `POST /campaigns/{campaignId}/publish` makes it live and matching creators can ask to join. Publishing tells other people, so it needs an `Idempotency-Key`.

## Next

- [Webhooks](https://yapfactory.com/developers/webhooks.md): hear about asks, yeses and drafts as they happen, instead of polling.
- [REST API reference](https://yapfactory.com/developers/api.md): every operation, with what it needs.
- [Errors](https://yapfactory.com/developers/errors.md): what each code means and what to do.
- [OAuth](https://yapfactory.com/developers/oauth.md): when your app acts for other brands.

---

HTML version: https://yapfactory.com/developers/quickstart
