# Send your first event

> Create a key under **Settings → API Keys** in Baker, choosing **Sending events only**. Post an event to `https://api.withbaker.com/v1/events` with the header `X-Baker-Mode: test`: it is checked and answered exactly like a real one and stores nothing. Read `rejected` in the answer, then remove the header to go live.

Source: https://withbaker.com/docs/quickstart · Updated 2026-11-23

## 1. Set your key

**To try it now**, use the public test key. It needs no account: every request is checked by the real API, exactly like yours, and nothing is stored.

```bash
export BAKER_EVENTS_KEY=bk_test_public
```

**To send your own events**, create your company's key:

1. In Baker, open the company the events belong to, then **Settings → API Keys**.
2. Give the key a label you will recognise later, such as the system that will hold it.
3. Under **What this key can do**, choose **Sending events only**, and press **Create**.
4. Put it in your server's environment as `BAKER_EVENTS_KEY`.

Or press **Sign in to use your key** at the top of withbaker.com/docs: every code sample and **Test Request** then carry your company's key, in that browser tab only.

> **Heads-up:** A sending key can post events and nothing else, but anybody holding it can post events as your company. Keep it on your server, never in a browser, an app or a repository.

## 2. Send a test event

The smallest event that counts names what happened and who it happened to. The `X-Baker-Mode: test` header makes this a rehearsal: authenticated, validated and answered exactly like a live request, and nothing is stored.

**cURL**

```bash
curl -X POST https://api.withbaker.com/v1/events \
  -H "Authorization: Bearer $BAKER_EVENTS_KEY" \
  -H "Content-Type: application/json" \
  -H "X-Baker-Mode: test" \
  -d '{
  "events": [
    {
      "event_name": "lead",
      "user_data": {
        "email": "laura@example.com"
      }
    }
  ]
}'
```

**Node.js**

```js
const response = await fetch("https://api.withbaker.com/v1/events", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.BAKER_EVENTS_KEY}`,
    "Content-Type": "application/json",
    "X-Baker-Mode": "test",
  },
  body: JSON.stringify({
    "events": [
      {
        "event_name": "lead",
        "user_data": {
          "email": "laura@example.com"
        }
      }
    ]
  }),
});

const { ok, data, error } = await response.json();
if (!ok) throw new Error(`Baker refused the request: ${error.code}`);
// A 200 can still refuse single events: log every one.
for (const refused of data.rejected) console.warn("Baker refused event", refused.index, refused.code, refused.message);
```

**Python**

```python
import os
import requests

response = requests.post(
    "https://api.withbaker.com/v1/events",
    headers={
        "Authorization": f"Bearer {os.environ['BAKER_EVENTS_KEY']}",
        "X-Baker-Mode": "test",
    },
    json={
        "events": [
            {
                "event_name": "lead",
                "user_data": {
                    "email": "laura@example.com",
                },
            },
        ],
    },
    timeout=10,
)

answer = response.json()
if not answer["ok"]:
    raise RuntimeError(f"Baker refused the request: {answer['error']['code']}")
# A 200 can still refuse single events: log every one.
for refused in answer["data"]["rejected"]:
    print("Baker refused event", refused["index"], refused["code"], refused["message"])
```

**PHP**

```php
<?php
$body = <<<'JSON'
{
  "events": [
    {
      "event_name": "lead",
      "user_data": {
        "email": "laura@example.com"
      }
    }
  ]
}
JSON;

$curl = curl_init('https://api.withbaker.com/v1/events');
curl_setopt_array($curl, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('BAKER_EVENTS_KEY'),
        'Content-Type: application/json',
        'X-Baker-Mode: test',
    ],
    CURLOPT_POSTFIELDS => $body,
]);
$answer = json_decode(curl_exec($curl), true);

if (!$answer['ok']) {
    throw new RuntimeException('Baker refused the request: ' . $answer['error']['code']);
}
// A 200 can still refuse single events: log every one.
foreach ($answer['data']['rejected'] as $refused) {
    error_log("Baker refused event {$refused['index']}: {$refused['code']}");
}
```

**Go**

```go
package main

import (
	"log"
	"net/http"
	"os"
	"strings"
)

const body = `{
  "events": [
    {
      "event_name": "lead",
      "user_data": {
        "email": "laura@example.com"
      }
    }
  ]
}`

func main() {
	request, _ := http.NewRequest("POST", "https://api.withbaker.com/v1/events", strings.NewReader(body))
	request.Header.Set("Authorization", "Bearer "+os.Getenv("BAKER_EVENTS_KEY"))
	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("X-Baker-Mode", "test")

	response, err := http.DefaultClient.Do(request)
	if err != nil {
		log.Fatal(err) // it may have arrived: retry with the same event_ids, which then count once
	}
	defer response.Body.Close()
	// Decode the answer and log every entry of data.rejected: a 200 can still refuse single events.
	log.Println("Baker answered", response.Status)
}
```

**Try it.** Send this to the real API with the public test key, `bk_test_public`: it is checked exactly like a company's request, answered the same way with `"mode": "test"`, and nothing is stored.

```bash
curl -X POST https://api.withbaker.com/v1/events \
  -H "Authorization: Bearer bk_test_public" \
  -H "Content-Type: application/json" \
  -d '{"events":[{"event_name":"lead","user_data":{"email":"laura@example.com"}}]}'
```

## 3. Read the answer

```json
{
  "ok": true,
  "data": {
    "mode": "test",
    "received": 1,
    "accepted": 1,
    "rejected": []
  }
}
```

`mode` says this was a rehearsal. `accepted` says how many events would have counted, and `rejected` lists every one that would not, with a `code` to branch on. A `200` is not the same as everything counting: always read `rejected`.

## 4. Go live

Remove the `X-Baker-Mode` header. The key, the address and the body stay the same, so nothing else about your integration changes. The answer now says `"mode": "live"`.

## 5. See it in Baker

Accepted events appear in Baker under **Measure → Events**, within seconds. Test sends and refused events are listed there too, under **All events → Test sends / Not counted**, with what you sent, so you can see what arrived without reading your own logs.

An event counts as a conversion once someone marks its name under **Conversions** in Baker. That applies to every event already sent under that name, so you can send first and decide later.

## Next steps

- [The full reference for POST /v1/events](https://withbaker.com/docs/events.md)
- [Match events to the right person](https://withbaker.com/docs/identity.md)
- [Retry safely with event_id](https://withbaker.com/docs/deduplication.md)
- [Ready-made bodies for common events](https://withbaker.com/docs/examples.md)

## Need help?

Write to support@withbaker.com: a person answers within 24 hours. The OpenAPI description is at https://withbaker.com/docs/openapi.json.
