# Authentication and signed URLs

> Authenticate Shotfly API requests with a secret key, or embed screenshots with signed URLs.

- **Server-side**: Pass the secret as `Authorization: Bearer sk_…` or `access_key=sk_…`.
- **Embeddable signed URLs**: Use the public key (`access_key=pk_…`) plus `signature`, an HMAC-SHA256 of the canonical query string made with your secret. Optionally add `expires` (Unix seconds).

Signed URLs can go straight into `<img src>` or `og:image` without exposing your secret.

## Signing

Take every parameter except `signature`, sort by name then value, encode names and values with RFC 3986, join as `name=value` with `&`, and compute a hex HMAC-SHA256 with your secret.

```js
import { createHmac } from 'node:crypto'

function signedUrl(params, publicKey, secret) {
  const all = { ...params, access_key: publicKey }
  const enc = (s) => encodeURIComponent(s).replace(/[!'()*]/g, (c) => '%' + c.charCodeAt(0).toString(16).toUpperCase())
  const canonical = Object.entries(all)
    .map(([k, v]) => [String(k), String(v)])
    .sort(([a, x], [b, y]) => (a === b ? (x < y ? -1 : 1) : a < b ? -1 : 1))
    .map(([k, v]) => `${enc(k)}=${enc(v)}`)
    .join('&')
  const signature = createHmac('sha256', secret).update(canonical).digest('hex')
  return `https://api.shotfly.dev/v1/screenshot?${new URLSearchParams({ ...all, signature })}`
}
```

---

Source: https://shotfly.dev/docs/authentication · Last updated 2026-11-27
