> ## Documentation Index
> Fetch the complete documentation index at: https://docs.magpiedb.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Magpie is a trace store for AI agents, used as a library (crate `magpiedb`, npm `magpiedb`, PyPI `magpiedb`) or as `magpie-server`, which takes OpenTelemetry (OTLP over HTTP on 127.0.0.1:4318 and gRPC on 127.0.0.1:4317 by default).
> Use only the commands, flags, routes and JSON shapes that these pages show. Do not guess a flag or a query field: if a page does not show it, say that the docs do not show it.
> Packages and server binaries are not published yet: build from source, as the quick start shows. Check the status note on the introduction page before you suggest `cargo add`, `npm install` or `pip install`.
> Only one process may write to a store folder at a time. For many apps, send traces to one `magpie-server`.

# API keys and TLS

> Turn on API keys and TLS for magpie-server, and run it behind a proxy.

## API keys

> [!WARNING]
>
> Without keys, the server has no authentication. It then listens on `127.0.0.1` only. Before you open it to other machines, turn on keys and TLS.

Make a key. The line for the key file goes to stdout; the key goes to stderr, and only this once. The file holds a SHA-256 of the key, not the key.

```sh theme={null}
magpie-server keygen --tenant default --rights read,write >> keys.jsonl 2> key.txt
KEY=$(tail -n 1 key.txt)
echo "${KEY:0:4}... is ${#KEY} characters"
```

```text theme={null}
mgp_... is 68 characters
```

Start the server with the key file:

```sh theme={null}
magpie-server --dir ./traces --auth keys --keys-file keys.jsonl
```

A request with no key gets `401`. With the key it works:

```sh theme={null}
curl -s -o /dev/null -w '%{http_code}\n' localhost:4318/v1/count -d '{}'
curl -s localhost:4318/v1/count -H "authorization: Bearer $KEY" -d '{}'
```

```text theme={null}
401
{"count":0}
```

* A key has the right `read` (queries and exports), `write` (OTLP, scores, deletes, purge), or both. An app that sends traces needs `write`; a dashboard needs `read`.
* A key with no right for a route gets `403`.
* The server reads the key file every 2 s. A new line works, and a removed line stops working, within about 2 s, with no restart. To revoke a key, delete its line.
* With one store (`--dir`), keys belong to the tenant `default`.
* OpenTelemetry SDKs send the key with `OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer%20mgp_..."` ([OpenTelemetry guide](https://docs.magpiedb.com/guides/opentelemetry.md)).

## TLS

`--tls-cert` and `--tls-key` turn on TLS on both ports. Give the certificate chain (PEM, the server's certificate first) and its private key (PEM). For a test, make a CA and a certificate for `localhost` with `openssl`. The server certificate must not be a CA (`basicConstraints=CA:FALSE`), or TLS clients refuse it:

```sh theme={null}
printf 'subjectAltName=DNS:localhost,IP:127.0.0.1\nbasicConstraints=CA:FALSE\n' > ext.cnf
openssl req -x509 -newkey rsa:2048 -nodes -keyout ca.key -out ca.pem -days 30 -subj /CN=my-test-ca 2> /dev/null
openssl req -newkey rsa:2048 -nodes -keyout key.pem -out server.csr -subj /CN=localhost 2> /dev/null
openssl x509 -req -in server.csr -CA ca.pem -CAkey ca.key -set_serial 1 -days 30 -extfile ext.cnf -out cert.pem 2> /dev/null
```

Start the server with keys and TLS:

```sh theme={null}
magpie-server --dir ./traces --auth keys --keys-file keys.jsonl --tls-cert cert.pem --tls-key key.pem
```

```sh theme={null}
curl -s --cacert ca.pem https://localhost:4318/v1/count -H "authorization: Bearer $KEY" -d '{}'
```

```json theme={null}
{"count":0}
```

* TLS 1.2 and 1.3 (rustls). The HTTP port then speaks HTTPS only.
* A new certificate needs a restart. At start the server prints the end date of the certificate. When the server's own certificate (the first of the file) is not valid now, the server does not start (exit code 1); another certificate of the file that is not valid now only gets a warning with its place in the file. One that ends within 30 days gets a warning at start and once a day (for a certificate of under 90 days: within a third of its time). After the end, an error line every hour.
* A failed TLS handshake is logged: the first at once, then at most one line a minute with a count and the reason (for example `received fatal alert: CertificateExpired`). If your clients cannot connect, look there.
* No client certificates (mTLS).
* A proxy that ends TLS (nginx, Caddy, a cloud load balancer) works too: run the server without `--tls-*` on a private address behind it, and pass the `authorization` header on. For gRPC, the proxy must speak HTTP/2 to the server.
* To accept connections from other machines: `--http-addr 0.0.0.0:4318 --grpc-addr 0.0.0.0:4317`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.