Install the Botmotive guard in EmDash
This page helps you add @botmotive/guard to an EmDash site, so it applies the fixes you choose in Botmotive.
The guard runs as EmDash's outer middleware, before EmDash opens its database, so a blocked bot is turned away before it costs your site any work. It enforces your rules: blocks, throttles, allow rules and path denials. It adds Botmotive's section to your robots.txt and, when the RSL license is on, serves /license.xml. It also sends a compact record of each request to Botmotive, so you don't need another log source. If you already send this site's logs from Cloudflare, turn the guard's logs off (see Logs below) so requests aren't counted twice.
What you need first
- A Botmotive account and a site. See Getting started.
- Your site ID, such as
my-shop. It is shown in Settings next to Site ID. - A token for the guard, which starts with
bm_. It needs both Fetch blocking rules and Send traffic logs. - An EmDash 1.x site on Cloudflare Workers or Node.
A new site starts with no token. Its install page makes one for you: open your site, choose EmDash (or click Connection steps in Settings), and click Make a token. The token is shown only once, and Botmotive keeps only a hash of it. You can also make one in Settings:
- Open your site in the dashboard and click Settings.
- Under Tokens, click New token. Name it after what will use it, such as
EmDash site, and click Make token. - Click Copy and store the token somewhere safe.
Install
Install the package:
npm install @botmotive/guard@^0.2.0Set your site ID and token where the site runs. On Cloudflare, store them as secrets:
npx wrangler secret put BOTMOTIVE_SITE npx wrangler secret put BOTMOTIVE_TOKENOn Node, set
BOTMOTIVE_SITE=my-shopandBOTMOTIVE_TOKEN=bm_...as environment variables. When either is missing, the guard does nothing, which keeps local development quiet.Create
src/botmotive.ts. Pick the version for where your site runs.On Cloudflare Workers:
import { env } from 'cloudflare:workers'; import { guardFromEnv } from '@botmotive/guard'; import { botmotive } from '@botmotive/guard/astro'; export const onRequest = botmotive(guardFromEnv(env, { ipHeader: 'cf-connecting-ip' }));On Node:
import { guardFromEnv } from '@botmotive/guard'; import { botmotive } from '@botmotive/guard/astro'; export const onRequest = botmotive(guardFromEnv());Tell EmDash to run it first. In
astro.config.mjs, addmiddlewareto youremdash()options:emdash({ // ...your database and storage settings middleware: { outer: './src/botmotive.ts' }, }),Deploy.
Settings that matter
Client IP. Rules match on the visitor's IP address. On Cloudflare Workers, cf-connecting-ip is set by Cloudflare's edge, and a visitor can't set it themselves, so the Workers version above reads it. On Node, the guard reads x-real-ip by default. Set BOTMOTIVE_IP_HEADER to the header your proxy sets: x-real-ip, x-forwarded-for, cf-connecting-ip, fly-client-ip or true-client-ip. The header is trusted as-is, so pick one that only your proxy can set.
What the guard sees. Middleware runs only for requests your site handles. Pages built ahead of time and static files that Cloudflare or your server answers directly never reach it, so they aren't checked or logged. To leave some paths alone, pass skip:
export const onRequest = botmotive(guardFromEnv(), {
skip: (context) => context.url.pathname.startsWith('/_emdash/'),
});
robots.txt. EmDash serves /robots.txt. The guard keeps EmDash's file and adds Botmotive's section after it, between # BEGIN Botmotive and # END Botmotive, with the License: line when the RSL license is on. EmDash's own Disallow AI training setting adds its rules to the same file, and both apply. EmDash lets browsers and caches keep robots.txt for a day, so the guard cuts that to 5 minutes and a new block reaches crawlers quickly. To leave robots.txt alone, pass { robots: false }.
/license.xml. When Serve an RSL license at /license.xml is ticked in Settings, the guard answers /license.xml itself.
Several isolates or servers. Throttle counters live in memory, per Worker isolate or Node process, so each one enforces its own share of a limit.
Logs. The guard records each request with your page's real status and sends the records gzipped every 10 seconds or 500 requests. On Cloudflare it sends them after the response, so visitors never wait. It never sends cookies, the Authorization header or request bodies, and it replaces the values of query parameters that look secret, such as token or password, with redacted. To turn logs off, set BOTMOTIVE_LOGS=off. Do this when Cloudflare Logpush already sends Botmotive this site's logs.
Domain. In Settings, fill in Domain under Your site's address, then click Save. Botmotive then reads your robots.txt and sitemap once a day.
Check it worked
Run the check with the same site ID and token. Put
BOTMOTIVE_TOKENin the shell's environment from your secret store or a .env file, not on the command line, where it lands in your shell history:BOTMOTIVE_SITE=my-shop npx -p @botmotive/guard@0.2.0 botmotive-checkIt prints
PASSorFAILfor the token, the API, your rules and a test log record. Within a minute, your site's page in the dashboard shows Guard connected: Node, last seen just now. EmDash sites use the Node guard, so the dashboard names it Node.Check traffic is arriving. Open your site in the dashboard and click the 15m tab. Once requests arrive, the dashboard says Receiving data with a count of requests so far.
Apply a block fix to a bot in the dashboard. Within a minute, request your site with that bot's user agent:
curl -i -A 'ExampleBot/1.0' https://example.com/You should see
403andForbidden.. A throttled visitor gets429with aRetry-Afterheader.
Updating and removing
- To update, run
npm install @botmotive/guard@latestand redeploy. - To pause the guard, delete the
BOTMOTIVE_SITEorBOTMOTIVE_TOKENsecret and redeploy. - To remove it, delete
middlewarefrom youremdash()options andsrc/botmotive.ts, then runnpm uninstall @botmotive/guard.
If something goes wrong
The guard fails open. If Botmotive can't be reached, the last rules stay in force. With no rules, every request goes through.
- Everyone or no one is blocked by an IP rule. The guard is reading the wrong IP header. See Client IP above.
botmotive rules: HTTP 401. The site ID or token is wrong, or the token was replaced.- robots.txt has no Botmotive section. Another file is answering
/robots.txtbefore EmDash does, such as apublic/robots.txt. Delete it.
For more, see Troubleshooting.