# Cookbook

> Worked recipes against the Funnel One API: push leads from your own signup form, send product events and start a motion from your own systems.

Complete examples you can adapt.

## Push leads from your own signup form

Your signup form posts to your own server. Your server forwards the lead to
Funnel One, so the API key never reaches the browser.

```ts
// Node 18+, Express. F1_API_KEY holds a key with leads.manage.
import express from 'express';
import { randomUUID } from 'node:crypto';

const app = express();
app.use(express.json());

app.post('/signup', async (req, res) => {
  const { email, firstName, lastName, company } = req.body;
  // One key per signup: a retry of THIS signup is not a second lead.
  const idempotencyKey = req.get('X-Signup-Id') ?? randomUUID();

  const f1 = await fetch('https://app.funnelone.ai/api/v1/leads', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.F1_API_KEY}`,
      'Content-Type': 'application/json',
      'Idempotency-Key': idempotencyKey,
    },
    body: JSON.stringify({
      email,
      first_name: firstName,
      last_name: lastName,
      company: { domain: company?.domain, name: company?.name },
      utm: { source: req.query.utm_source, medium: req.query.utm_medium, campaign: req.query.utm_campaign },
    }),
  });

  // 201: created. 409 duplicate_request: already sent, which is fine on a retry.
  if (f1.status !== 201 && f1.status !== 409) {
    console.error('Funnel One lead failed', f1.status, await f1.text());
  }
  res.status(204).end();
});
```

Things to know:

- A `429` means you hit the key's per-minute limit, or the workspace's 60
  leads a minute. Wait `Retry-After`
  seconds and send it again with the same `Idempotency-Key`.
- Do not block your own signup on Funnel One. Log a failure and retry later.

## Start a motion from your own systems

When something happens in your product (a trial starts, an order ships), start
a motion for that company. The motion needs an **API call** trigger in its
entry settings, and the key needs `motions.start`.

```sh
curl -X POST https://app.funnelone.ai/api/v1/motions/42/start \
  -H "Authorization: Bearer $F1_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: trial-started-1234" \
  -d '{"companyId": 1234}'
```

A `202` with `"status": "queued"` means it was accepted; Funnel One decides the
entry with the same checks as every other trigger. Sending the same key again
returns the decision once it is made.

## Send product events and signal on them

Tell Funnel One when a customer does something in your product, then build a
[custom signal](https://dev.funnelone.ai/rest/custom-signals/) on it. The key needs `events.send`.

```ts
// Node 18+. Call it from wherever the event happens in your product.
export async function sendF1Event({ id, name, domain, properties }) {
  const res = await fetch('https://app.funnelone.ai/api/v1/events', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${process.env.F1_API_KEY}`,
      'Content-Type': 'application/json',
    },
    // `id` is yours and unique per event: a retry with the same id is a duplicate, never a second event.
    body: JSON.stringify({ id, name, company: { domain }, properties }),
  });
  if (res.status === 429 || res.status === 503) {
    // Retry later with the SAME id, after the Retry-After seconds.
    return { retryAfter: Number(res.headers.get('Retry-After')) };
  }
  return res.json(); // 202: { id, status: 'accepted' | 'unmatched' | 'duplicate' }
}

await sendF1Event({ id: 'trial-4812', name: 'trial_started', domain: 'acme.com', properties: { plan: 'pro' } });
```

Things to know:

- `unmatched` means no company in your workspace has that domain. The event is
  kept for 30 days and does nothing. Funnel One never creates a company from an
  event.
- Never put an email, a name or a phone number in `properties`; the event is
  refused. Name the person in `contact` instead.
- Sending many at once? Use `POST /api/v1/events/batch` with up to 100 events.

## Check why an event did nothing

An event that matched no company is kept for 30 days as `unmatched`. List the
recent ones to see which domains your systems send that Funnel One does not
know yet:

```sh
curl "https://app.funnelone.ai/api/v1/events?status=unmatched&limit=100" \
  -H "Authorization: Bearer $F1_API_KEY"
```

Each row carries the `domain_hint` the event named. Add those companies to
Funnel One (or send them as [leads](https://dev.funnelone.ai/rest/)), and the next events for them
match. Keep passing `nextCursor` as `cursor` until it is `null`.

Source: https://dev.funnelone.ai/resources/cookbook/
