# Signed URLs

A **signed URL** is a link of your app that carries proof you made it. A link that changes state, such as unsubscribe, confirm or download, must not be forged, edited or used forever; and the page behind it often has no session to check, because the link was opened from a mail.

`signURL(path, options)` returns the path with an expiry and a signature added to its query. `verifySignedURL(event, options)` checks the request's own URL when the link comes back.

## Make a link

````ts [security/signed-url-sign.ts]
import { signURL } from "hamur/server"

export function unsubscribeLink(subscriberId: string): Promise<string> {
  return signURL(`/unsubscribe/${subscriberId}`, { purpose: "unsubscribe", ttl: 7 * 24 * 3600 })
}
````

`path` is the path of your app that the request will carry, `app.baseURL` included, starting with `/`. Anything else throws: `signURL takes an app path that starts with "/" (got "https://x")`. The result is the path with `expires=<unix seconds>&signature=<base64url>` appended; put your own origin in front of it for a mail. An `expires` or `signature` already in the path is replaced.

| Option    | Default | Meaning                                                                                                  |
| --------- | ------- | -------------------------------------------------------------------------------------------------------- |
| `ttl`     | `3600`  | whole seconds the link lasts; any other value throws `signURL ttl must be a positive integer of seconds` |
| `purpose` | `""`    | a name for what the link is for; a link signed for one purpose does not verify for another               |

## Check a link

````ts [security/signed-url-verify.ts]
import { defineHandler, verifySignedURL } from "hamur/server"

declare function removeSubscriber(id: string): Promise<void>

export const unsubscribe = defineHandler({
  route: "/unsubscribe/:id",
  method: "GET",
  async handler({ id }: { id: string }, { event }) {
    await verifySignedURL(event, { purpose: "unsubscribe" })
    await removeSubscriber(id)
    return { unsubscribed: true }
  },
})
````

`verifySignedURL` returns when the signature is good and throws a 403 `Invalid or expired link` otherwise. The answer is the same for every cause, so it tells an attacker nothing: a missing or repeated `signature` or `expires`, a signature that is not the signature of this path and query, a link past its `expires` (read from the server's clock) and a link signed for another `purpose`. Call it first in the handler, before any state changes.

## What is signed

The signature covers the path and the whole query, sorted by key, without the `signature` itself. A changed or added parameter fails, and the order of the parameters does not matter. The path is the request's own, `baseURL` included, so sign the path as the browser will request it.

The key is derived from the app key `HAMUR_KEY` and the `purpose`, and `verifySignedURL` tries every key of a rotated `HAMUR_KEY` list, so links made before a key rotation keep working until the old key is dropped ([Sessions and security](/sessions-and-security)). The first key signs; every key verifies. Without `HAMUR_KEY` a built server throws at the first use, as for sessions.

## Per preset

Nothing in a signed URL touches a store or a platform feature: it is an HMAC computed in the server, so it works the same on `bun`, `node`, `cloudflare`, `vercel`, `netlify` and the rest. Every instance of the app needs the same `HAMUR_KEY`.

A signed link is a bearer: whoever has it can use it until `expires`. Keep `ttl` as short as the use allows, give each use its own `purpose`, and do not put one in a page that others can read. It is not a login; it does not say who opened it.

The signatures are in the [server API reference](/api-reference/server#signurl-verifysignedurl).
