# Server

Send events from your backend with an API key, for webhooks, background jobs and purchases confirmed on the server.

Send events from your backend when the browser is not involved: a webhook, a background job, or a purchase confirmed on the server. Use the Node.js package, or one HTTP request from any language.

## Create an API key

Create a key under **Integrations → API keys** in your organization settings and tick Send events (`events:write`).

> **Warning:** A key with Send events can write events for every site in your organization. Keep it in an environment variable, never ship it to the browser, and revoke it if it leaks.

## Send an event from Node.js

The package runs on Node.js 22.12 and newer, Bun and Deno, and has no dependencies.

```bash
npm install @ultanimus/node
```

Keep the key in `ULTANIMUS_API_KEY`, then send events from anywhere on your server, for example a payment webhook:

```ts
import { Ultanimus } from '@ultanimus/node'

const ultanimus = new Ultanimus({ site: 'YOUR_SITE_ID' })

await ultanimus.track('purchase', { user: order.userId, id: order.id, props: { plan: order.plan } })
```

`track` takes the event's name, then the [fields](#the-fields) below. It never rejects, so a failed event cannot take your webhook down with it. It resolves to `{ ok: true }` or `{ ok: false, error }`, where `error.code` is one of the [codes](#the-answer) below:

```ts
const result = await ultanimus.track('signup', { user: user.id })
if (!result.ok) console.error(result.error.code, result.error.message)
```

An event that gets no answer is sent again, up to twice, with the same `id`, so it never counts twice. Either way `track` answers within 3 seconds. On a serverless platform, `await` the call or hand it to `waitUntil`, so the function does not end before the event leaves.

## Send an event from any language

`POST https://api.ultanimus.com/v1/events`

```bash
curl -X POST https://api.ultanimus.com/v1/events \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"site":"YOUR_SITE_ID","name":"purchase","user":"u_8f2k1x","id":"ord_4410","props":{"plan":"pro"}}'
```

## The fields

- `site` (string, required): Your site ID, from the site's settings. The site has to belong to the key's organization.
- `name` (string, required): The event name, such as `purchase`. Up to 120 characters.
- `user` (string | number): Your own ID for the person, the same one you pass to [`identify()`](/docs/tracking/identify) in the browser. Up to 256 characters. Leave it out when the event belongs to no one.
- `id` (string | number): An ID for the event, such as the order ID. If another event with the same ID arrived in the last 5 minutes, this one does not count, so a webhook that fires twice counts once. Up to 64 letters, digits, dashes and underscores.
- `props` (object): Properties to filter and break the event down by, such as the plan. Up to 30, each a string of up to 512 bytes, a number, or true or false.
- `url` (string): The page the event belongs to, on the site's domain or a subdomain. Required when `name` is `pageview`.

A body can be up to 8 KB. Keep personal details such as names and email addresses out of every field. Use an ID from your own database for `user`.

## The answer

A counted event gets `202`. So does a repeat of an `id` from the last 5 minutes, with a message that says it did not count, so retrying a request never does harm.

```json
{
  "status_code": 202,
  "data": null,
  "message": "Event received."
}
```

Failures use the envelope from the [API overview](/docs/api/overview#responses). These are the ones to expect here:

| Status | Code | When |
|---|---|---|
| 401 | `ULT-1220` | The API key is missing, wrong or revoked. |
| 403 | `ULT-1203` | The organization's plan does not include API access. |
| 403 | `ULT-1219` | The organization is suspended. An owner can read why in the dashboard. |
| 403 | `ULT-1221` | The key does not have Send events (`events:write`). |
| 403 | `ULT-1302` | `url` is not on the site's domain. |
| 404 | `ULT-1210` | No site in the key's organization has that ID. |
| 413 | `ULT-1904` | The body is larger than 8 KB. |
| 422 | `ULT-1900` | A field failed a check. `validation_errors` says which. |
| 429 | `ULT-1902` | More than 6,000 events in a minute from one key, or for one user. Retry in the next minute. |
| 429 | `ULT-1303` | The organization is past its monthly event limit. The event does not count. |
| 503 | `ULT-9003` | Events cannot be stored right now. Retry with backoff. |

## How server events count

Server events count toward your monthly event limit like browser events. Goals, funnels and the Events page include them.

- **With `user`.** The event counts for the same visitor as the browser where you called `identify()` with that ID. If their browser sent an event in the last 30 minutes, the event joins that visit and takes its source, country and device. If not, it counts on its own and adds nothing to visits, bounce rate or visit duration.
- **Without `user`.** All of the site's server events on one day count as one visitor, with no source, country or device.

Server events skip the bot check that browser events go through, since your own server sends them. [Signed-in users](/docs/tracking/identify) explains how a user ID ties the two sides together.

## Plan limits

| Feature | Glance | Insight | Foresight |
|---|---|---|---|
| API access | Not included | Included | Included |
