Help

Latch is a proxy you run on the same machine as the API. It allows the call or returns 429. The desk is optional, and sync stays off until you turn it on.

1. Leave the API where it is

Start the API the way you already start it. Write down the IPv4 address and port it listens on. Latch connects to that address over plain HTTP. A name is fine when it resolves to an IPv4 address.

The public site, if you have one, stays on its own port. Only the API paths should be sent to Latch. Latch listens on a port of its own, usually 127.0.0.1, so only the reverse proxy on that machine can reach it.

2. Write a policy file

The file needs one service line. A limit line names the rule, counts requests or tokens, picks sliding_window or token_bucket, says who the counter belongs to, then the limit and the window in seconds. Identity fields are api_key, ip, tenant, route, and model, joined with commas when a counter is shared across more than one.

service my-api
limit requests_per_minute requests sliding_window api_key 120 60
abuse key_fanout 30 3600
abuse ip_fanout 50 3600
abuse retry_storm 30 60
abuse token_daily_cap 100000 86400

That file allows 120 requests a minute for each API key. The four abuse lines are separate. key_fanout 30 3600 denies when one key is used from more than 30 addresses in an hour. ip_fanout 50 3600 denies when one address uses more than 50 keys in an hour. retry_storm 30 60 counts only calls that send X-Latch-Retry: true. token_daily_cap counts the number in X-Latch-Tokens.

Check the file before you serve it.

latch check --policy policy.txt

A good file prints policy ok and the counts of limits and abuse rules.

3. Start the proxy

--listen is where callers connect. --upstream is the API from step 1. --policy is the file from step 2.

latch serve \
  --listen 127.0.0.1:8081 \
  --upstream 127.0.0.1:8080 \
  --policy policy.txt

Point the public reverse proxy at the listen port for the API paths only. Then call through that public name, or through the listen port from the same machine.

curl -sS -D - \
  -H "Authorization: Bearer YOUR_KEY" \
  http://127.0.0.1:8081/v1/health

An allow is the API's own response. A deny is HTTP 429 and a JSON body with decision, rule, observed, and limit. The request body is not kept. The largest body Latch will forward is 64 MB, and it must have a Content-Length.

The key is Authorization: Bearer or X-Api-Key. Optional headers are X-Tenant, X-Model, X-Latch-Tokens, and X-Latch-Retry. When the reverse proxy sends X-Forwarded-For, Latch uses the last IPv4 address in that header as the caller.

If the API is down, Latch returns 502 and {"decision":"allow","error":"upstream"}. The call was allowed and the forward failed.

4. Turn on the desk

Choose Pro or Distributed on the home page and pay. The desk then shows a proxy token. That token is only for sync. It is not the API key your callers send. The free proxy does not need an account.

Stop the proxy and start it again with sync. Pick an instance name you will recognize in the decision list.

latch serve \
  --listen 127.0.0.1:8081 \
  --upstream 127.0.0.1:8080 \
  --policy policy.txt \
  --sync https://latch-control.grbsystem.com \
  --sync-token YOUR_PROXY_TOKEN \
  --instance api-a

Wait until the log says sync policy applied. The desk policy replaces the file on the next sync, about every 5 seconds. Saving a policy on the desk does not change a proxy that was started without --sync.

The decision list shows allow or deny, the rule, the key, the caller address, the route, and the time. Abuse shows only the four abuse rules. Summary totals every stored decision, then splits them by key, by route, and by hour.

5. Shared budget and retention

Leave the shared budget empty when one proxy is enough. Local limits still apply. A number there is one pool for every proxy using that account. The slice is how many requests one proxy may hold at a time. When the pool is spent, the next call is a 429 with rule lease. Setting the budget again refills the pool and clears the slices already handed out.

Days to keep starts at 30. Older decisions are deleted from the summary and from the database. Pro includes 30 days and 250,000 stored decisions a month. Distributed is the plan with a shared budget, 90 days, and 1,000,000 stored decisions a month.

What stays on your machine

Until sync is on, the decisions stay in the proxy. The control plane stores the account email, the policy, and, after sync, the decisions. It does not store request bodies. One account's decisions are not used to change another account's rules.