# Authentication

> One page of the LogDuck documentation. Every page is listed in https://logduck.com/llms.txt, and https://logduck.com/llms-full.txt is all of them in one file.

All API requests must be authenticated using an API key. Include your API key in the `X-API-Key` header.

## Example

```bash
curl -X POST https://api.logduck.com/v1/events \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Idempotency-Key: 550e8400-e29b-41d4-a716-446655440000" \
  -H "Content-Type: application/json" \
  -d '{"type": "user.signup", "source": "my-backend"}'
```

> **Warning: Keep Your API Key Secure**
> Never expose your API key in client-side code. Always make API calls from your server.

## Managing API Keys

You can create, view, and revoke API keys in your project settings. Each project can have multiple API keys, which is useful for rotating keys or having separate keys for different environments. A revoked key is refused within about ten minutes, so rotate the caller first.

## Server or client keys

Creating a key asks whether it will be held by a `server` or by a `client` — a backend you run, or an app on someone's device. The answer is recorded on the key and copied onto every event it posts, as `sourcetype`.

It matters because the two are indistinguishable once an event is stored, and they mean opposite things on a map: a client key's location is roughly where your user is, while a server key's is where your servers are hosted. Analysis can filter on it for exactly this reason. For a client key the request's IP address is resolved to a city and then discarded, and the browser's user-agent string is reduced to a device class and a browser family — neither is stored, because a device's address and its full user agent are personal data. Changing a key's type later takes effect within about ten minutes.

## Allowed origins for client keys

A client key ships inside your page or app, where anyone can read it. To stop another website using it, give the key a list of allowed origins: `https://example.com`, or `https://*.example.com` to cover the domain and every subdomain. Browsers send the page's origin with every request, and a request from an origin not on the list is refused with a 403 that names it.

Native apps, command-line tools and some webviews send no Origin header at all. For a key shipped in one of those, turn on "accepts requests without an Origin header" instead. With no origins listed, that key then refuses any browser, which is what protects an app's key from being embedded in a web page. A client key with no origins and that setting off accepts requests from anywhere, and the dashboard marks it unrestricted.

The origin check guards against other websites, not against scripts: a script can send any header, which is what the rate limit is for. Entries may use any scheme, so a hybrid app can list capacitor://localhost. Never put a server key in a page. Changes to a key's origins take effect within about ten minutes.
