# Authentication

> Every request needs a key. Public keys are for browsers and bound to your domains, secret keys are for servers.

Source: https://findort.ch/en/docs/authentication/

## Two kinds of keys

| | Public `pk_…` | Secret `sk_…` |
|---|---|---|
| Use | Browser, HTML, Web Component, mobile web | Servers, cron jobs, scripts |
| Origin check | yes, listed origins only | no |
| Safe in HTML | yes | never |
| Without an `Origin` header (curl, server) | `403 origin_not_allowed` | works |

A public key is not a secret. It sits in your page's source and anyone can read it. That is fine, because the API only accepts it for requests from your domains.

A secret key checks no origin. Whoever has it can use up your quota. Keep it in an environment variable, never in code, a repository or a frontend bundle.

> **Important:** if a secret key leaks, create a new one in the dashboard and delete the old one.

## Sending the key

Preferably in the `Authorization` header:

```http
Host: api.findort.ch
Authorization: Bearer sk_…
```

Where you cannot set headers, public keys also work as the `key` parameter:

```bash
```

If you send both, the header wins. Secret keys (`sk_`) are accepted in the header only: in a URL they end up in logs and browser histories, and the API answers `400 secret_key_in_url`. The libraries and the Web Component always use the header.

## Allowed origins

For a public key you keep a list of allowed origins. The API compares it with the request's `Origin` header. If that header is missing, it takes the origin from the `Referer`.

| Entry | Matches | Does not match |
|---|---|---|
| `https://shop.example.ch` | exactly this origin (scheme, host and port) | `http://shop.example.ch`, `https://www.example.ch` |
| `*.example.ch` | `example.ch` and every subdomain like `shop.example.ch` or `a.b.example.ch`, over `http` and `https` | `example.com`, `shop.example.ch:8443` |
| `shop.example.ch` | host `shop.example.ch` with any scheme | `www.shop.example.ch` |
| `http://localhost:5173` | your local dev server | other ports |
| `*` | every origin | |

A `*.` wildcard only covers addresses without a port. For a dev server with a port, add the full origin, for example `http://localhost:8080`.

> **Tip:** `*` makes a public key usable on any website. Use it for a quick test at most.

If the origin does not match, the API answers with `403` and tells you which origin it saw:

```json
  "error": {
    "code": "origin_not_allowed",
    "message": "Origin https://staging.example.ch is not allowed for this key"
  }
}
```

## CORS

The API accepts requests from any website (`Access-Control-Allow-Origin: *`). Only the key decides whether a request gets through. Requests with an `Authorization` header make the browser send a preflight request (`OPTIONS`). The API answers it without a key and lets the browser cache it for 24 hours.

## Rate limit headers

Every response to a valid request carries these headers:

| Header | Meaning |
|---|---|
| `X-RateLimit-Limit` | requests per minute for this key |
| `X-RateLimit-Remaining` | how many requests you have left right now |
| `Retry-After` | only with `429 rate_limited`: seconds until the next try |

The limit refills continuously, not after a full minute. If you send a burst, you only wait a few seconds afterwards. Browsers can read the headers, the API exposes them via CORS.

```http
X-RateLimit-Limit: 600
X-RateLimit-Remaining: 0
Retry-After: 2
```

The per-minute limit is separate from your plan's monthly quota. Both are covered in [limits](https://findort.ch/en/docs/limits/).
