# Best practices

> Where to call Heimdal, how long to wait for it, what to do when it does not answer, and which signals deserve a hard block.

## Call it once, on your server, before you create the account

Run the check in your signup handler, after you have validated the form and before you write the user to your database or send any email. A refused signup then costs you nothing.

- **Send both values.** The email catches disposable inboxes, the IP address catches Tor, proxies and server networks. Either alone sees half the picture.
- **Check once per signup.** Do not check on every page view or every login, and do not store the answer to reuse later: the lists change every hour.
- **Stay on the server.** The API key must never reach a browser. See [Authentication](https://heimdal.codebrewerz.com/docs/authentication/#keep-the-key-on-your-server).
- **Prefer `POST`.** A JSON body keeps email addresses out of URLs and access logs.

## Send the visitor's real IP address

Behind a load balancer or a CDN, the address your code sees is the proxy's. Send the address of the visitor:

- Read it from the header your own proxy sets, and only trust that header when the request really came through your proxy. A visitor can write anything into `X-Forwarded-For`.
- If the value might be malformed, validate it or leave `ip` out. An invalid `ip` answers `400` and takes the email check down with it.
- If you have no address, send the email alone. `ip` comes back as `null`.

In development the address is usually `127.0.0.1`. That is fine: a private address has no network, so none of the IP signals fire.

## Set a timeout

A signup must not hang because of a network problem between you and Heimdal. Give the request a short timeout and move on when it expires. Two seconds is generous.

```js
const response = await fetch("https://heimdal.codebrewerz.com/api/v1/check", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.HEIMDAL_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ ip, email }),
  signal: AbortSignal.timeout(2000),
});
```

## Fail open

When the check does not produce an answer, let the signup through. Turning away a real customer because of an outage is worse than letting one doubtful signup pass, and you still have email verification behind it.

| What happened            | What to do                                                                   |
| ------------------------ | ---------------------------------------------------------------------------- |
| Timeout or network error | Allow the signup. Log it.                                                    |
| `429 rate_limited`       | Allow the signup. Log it; if it keeps happening, find the runaway caller.    |
| `503`                    | Allow the signup. Log it.                                                    |
| `401` or `402`           | Allow the signup, and alert yourself: the key or the subscription is broken. |
| `400`                    | Allow the signup, and fix the request your code builds.                      |

Do not retry inside the signup request. A retry makes the visitor wait, and failing open already covers the gap.

Never show Heimdal's error messages to your users. They are written for your logs.

Heimdal's own signup does exactly this: if the check cannot answer, the signup goes ahead and the failure is logged.

## Choose a policy

`block` is `true` when any signal fires. That is the strictest policy there is, and for most products it refuses too many good customers. A VPN alone sets it.

Start from the policy Heimdal uses on its own signup, and tighten it only where you see abuse:

| Signal             | Suggested action     | Why                                                                                 |
| ------------------ | -------------------- | ----------------------------------------------------------------------------------- |
| `email.disposable` | Refuse               | A throwaway inbox has no purpose at signup other than avoiding you.                 |
| `ip.tor`           | Refuse               | Few genuine signups arrive through Tor; a lot of abuse does.                        |
| `ip.proxy`         | Refuse               | Open proxies are abused by design.                                                  |
| `custom.matched`   | Refuse               | You put the entry there.                                                            |
| `ip.vpn`           | Log, or add friction | Plenty of legitimate people are on a VPN, developers and remote workers among them. |
| `ip.bad_isp`       | Log, or add friction | Company networks and cloud desktops live here too.                                  |

```js
function refusalFor(result) {
  if (result.email?.disposable) {
    return "Disposable email addresses can't be used to sign up.";
  }
  if (result.ip?.tor || result.ip?.proxy) {
    return "Sign-ups from this network aren't allowed. Contact support if this is a mistake.";
  }
  if (result.custom.matched.length > 0) {
    return "This sign-up can't be completed. Contact support if this is a mistake.";
  }
  return null;
}
```

:::caution
Do not block on `vpn` alone unless you mean it. If your product has a specific reason to refuse VPN users, such as regional licensing or a history of trial abuse through VPNs, make that a deliberate choice and tell the visitor why they were refused.
:::

### Between allowing and refusing

A signal does not have to end the signup. For `vpn` and `bad_isp` in particular, consider a step that costs an honest visitor little:

- Require email verification before the account can do anything.
- Show a CAPTCHA.
- Hold back the free trial or the free credits until a payment method is added.
- Put the account in a queue for manual review.

## Tell the visitor what to fix

A refusal the visitor can act on loses fewer customers:

- For a disposable email, say so. People with a real address will use it.
- For a network signal, say that signups from this network are not allowed, and give a way to reach you. Do not say which list they are on.

## Log what you decided

Store `reasons`, and `ip.asn` and `ip.isp` when present, next to the signup: for the ones you refused and for the ones you let through. A month of that data shows whether your policy is too strict or too loose.

The dashboard keeps this for you: counts per reason over time, and a log of recent checks.

## Look after your keys

- One key per service and environment.
- Rotate a key by creating the new one first. A revoked key can keep working for up to 60 seconds.
- A leaked key lets someone else use your subscription and probe your custom lists. Revoke it and create a new one.
