beagle-service: the server side of Sign in with Beagle
An npm package for the server of a site whose visitors sign in with their Beagle identity: sign-in and sessions, your own user records, and the site's own identity on the network. Written for version 0.3.0.
November 2026What it is
@decentnetwork/beagle-service is the server that the browser calls of @decentnetwork/beagle-connect talk to. A visitor's identity is a key pair made on their own device, and the userid is its public half. Your site learns who they are from a signature, keeps its own record of them, and can message them from the site's own identity. There is no account server in between.
Use it when you would otherwise write the server check from Add Sign in with Beagle to your app yourself, plus the nonce store, sessions and a users table around it. This page is for version 0.3.0, the latest tag on npm. It needs Node 20 or later and works from import and from require.
| Part | What it does |
|---|---|
auth | Sign-in on the server: single-use nonce, exact origin check, signature check, sessions with expiry and sign-out, a rate limit. Routes for node:http, Express and Next.js. |
users | One record per userid: visitor, registered or blocked. Registration fields you choose, block and unblock. |
agent | The site's own identity on the network, in your process: accepts friend requests by a policy, sends and receives messages. |
Quick start
mkdir my-site && cd my-site
npm init -y
npm install @decentnetwork/beagle-serviceThe smallest server: the sign-in routes under /api/auth, and one page of your own that answers only to someone signed in. Save it as server.mjs.
import http from "node:http";
import { createBeagleService } from "@decentnetwork/beagle-service";
const svc = await createBeagleService({
origins: ["http://localhost:3000"], // exact: scheme, host and port
dataDir: "./data",
});
const auth = svc.nodeHandler({ prefix: "/api/auth" });
http.createServer(async (req, res) => {
if (req.url.startsWith("/api/auth/")) return auth(req, res);
const who = await svc.auth.authenticate(req); // never throws; decide on who.ok
res.writeHead(who.ok ? 200 : who.status, { "content-type": "application/json" });
res.end(JSON.stringify(who.ok ? { hello: who.user.userid } : { error: who.error }));
}).listen(3000);How to tell it works. The nonce route answers, your own page refuses a stranger, and a script that signs in with a throwaway key gets a session and appears in the user list. On your own machine the server also prints one line saying it cannot rate-limit requests from the machine itself; that is expected locally.
node server.mjs &
curl http://localhost:3000/api/auth/nonce # {"ok":true,"nonce":"…","expiresIn":120}
curl http://localhost:3000/ # {"error":"not-signed-in"}
# sign in with a throwaway key, the way a script or a test does
cp node_modules/@decentnetwork/beagle-service/examples/sign-in-from-script.mjs .
node sign-in-from-script.mjs http://localhost:3000 # signed in as <userid>
npx beagle-service users listThe sign-in button. The package ships a one-file site with its page, examples/minimal-site: two buttons, one for a Beagle installed on the visitor's computer and one for Beagle in the browser at app.beagle.chat. Either is just a place the visitor's identity lives. The page uses signIn, readLaunch and addFriend from beagle-connect. Stop the first server before you start it, since both use port 3000.
npm install @decentnetwork/beagle-connect esbuild
cp -R node_modules/@decentnetwork/beagle-service/examples/minimal-site .
node minimal-site/server.mjs # then open http://localhost:3000The three parts
auth. svc.nodeHandler() (or expressHandler(), route(name), nextApiHandler(), fetchHandler()) serves nonce, sign-in, launch, me, logout and register. In your own routes, call await svc.auth.authenticate(req) as server.mjs does above. It never throws. Decide on who.ok and nothing else.
users. Records live in <dataDir>/users.json unless you pass a store of your own. register: { required: ["email"] } makes a visitor registered once they give those fields. The operator's command works beside the running server, and a block takes effect at once: sign-in answers 403 and that userid's sessions end.
npx beagle-service users list
npx beagle-service users show <userid>
npx beagle-service users block <userid> spam
npx beagle-service users unblock <userid>agent. Add agent to the options and the site gets its own identity, with its key in <dataDir>/agent/peer.save. People add its address like any contact. Nothing is delivered either way until a friend request is accepted. A request needs the visitor's address, not only their userid.
import { createBeagleService } from "@decentnetwork/beagle-service";
const svc = await createBeagleService({
origins: ["http://localhost:3000"],
dataDir: "./data",
agent: { nickname: "My Site", accept: "signed-in", befriendOn: "register" },
});
console.log("add this address in Beagle:", svc.agent.address);
svc.agent.onMessage(({ user, text, reply }) =>
reply(user ? `You said: ${text}` : "Sign in on the site first."));Deploying
import { createBeagleService } from "@decentnetwork/beagle-service";
const svc = await createBeagleService({
origins: ["https://your.app"], // scheme, host and port: what the address bar shows
rateLimit: { trustProxy: ["loopback"] }, // nginx or Caddy on the same machine
dataDir: "./data", // keep it: users, sessions, the agent's key
});
console.log(svc.auth.origins());
await svc.close();- `origins` lists exactly what the browser's address bar shows: scheme, host and port.
http://localhost:3000counts only if you list it. A bare hostname, a path or a*stops the service at start. - Behind a reverse proxy, set `trustProxy`. Sign-in is rate-limited per visitor (30 a minute by default). With nginx or Caddy on the same machine and nothing set, sign-in is not limited at all. With a proxy on another machine, all visitors share its address. Name the proxy:
["loopback"], or its address. - Keep the data folder, out of git and backed up. It holds the users, used nonces and revoked sessions, the session secret and the agent's key. Lose
agent/peer.saveand the site's identity is gone. - Node 20 or later. Several servers on one set of users need one
sessionSecret. The README covers that, and Next.js.
Common mistakes
- 403 `origin-not-allowed`. The page's origin is not on the list exactly.
http://127.0.0.1:3000is nothttp://localhost:3000, andhttpsis nothttp. - A script that signs in without `Origin`. A browser sends the header itself; a script has to send it, and sign the same origin. Without it the answer is 403
origin-not-allowed. The server never takes the origin from the body. - Treating a failed session read as public.
authenticate()answersok: falsewith a status for no session, a blocked user and a store it could not read alike. Wrapping it intry { … } catch { /* public */ }, or forgetting theawait, serves a private page to anybody.
More
Everything else is in the package README: every route and error code, registration and the visitor limit, your own user store, onSignIn and onSignOut, Next.js with or without NextAuth, sending with delivery receipts, and upgrading from 0.2.0. In its own Quick start, the last line (await svc.users.block(userid, "spam")) uses a userid the snippet never defines, so that snippet stops there with a ReferenceError. The examples above are the ones that run as written.