Node.js SDK
The fnlb package is the Node.js and Bun SDK for FNLB. You use it to run bots from your own process: a small script on your laptop, a long-running service, or a self-hosted cluster. This page covers install, authentication, and start() / stop() — how you bring local shard processes online and configure shards. After start(), you work with bots through the SDK itself: listen for events, read and set party meta, send chat, run commands, and call fnlb.api. That is documented in the SDK guides, not only for self-hosting. Any project that calls start() uses the same APIs. The generated symbol reference is the SDK Reference. The machine-readable spec is /sdk.json.
SDK guides
- Overview
Local shards versus the HTTP API, and how the guides fit together.
- Events
Ready, chat, friends, party, and cosmetic events.
- Bots and parties
Bots, members, and party or member meta.
- Chat and commands
Party chat, whispers, friends, and
runCommand. - HTTP client
fnlb.apifor bots, categories, presets, and cloud commands. - Category config
Every key for
overrideCategoryConfig. - SDK Reference
Every class, method, and type.
Requirements
- Node.js 22 or newer (or Bun). The examples use top-level
await, so run them as ES modules (.mjs, or a project with"type": "module"). - An FNLB account with at least one bot. Create and manage bots at app.fnlb.net.
- An API token or OAuth2 access token — see Authentication.
Installation
npm install fnlb@latestUsing Bun? Install with:
bun install fnlb@latestAuthentication
start() uses your credential to read categories and bots, then opens a gateway connection for the processes it spawns. Pass any supported credential as apiToken or token — the two options are interchangeable.
| Type | Format | Notes |
|---|---|---|
| API token | FNLB_... | From app.fnlb.net/account. See Authenticating. |
| OAuth2 access token | FNLBOA2AT_... | Needs the bots.run, categories.read, and bots.read scopes. See OAuth2. |
await fnlb.start({
token: 'FNLBOA2AT_...',
categories: ['your-category-id']
});Quick start: one bot
import FNLB from 'fnlb';
const fnlb = new FNLB();
await fnlb.start({
apiToken: 'your-api-token',
categories: ['your-category-id']
});Category ids and bot ids are shown on a bot's page at app.fnlb.net/bots under About this bot, and via GET /categories and GET /bots.
Options reference
Constructor (FNLBConfig)
| Option | Type | Description |
|---|---|---|
clusterName | string | Name shown for your cluster in logs and on the gateway. |
fnlbPath | string | Directory used for the .fnlb download cache. Defaults to the current working directory. |
channel | 'stable' | 'beta' | 'dev' | Default release channel. Defaults to stable. |
updateIntervalMs | number | How often (in milliseconds) the SDK checks for package updates. |
maxDownloadRetries | number | Maximum download retries before giving up. |
maxBackoffMs | number | Maximum backoff between retries. |
onLogMessage | (message: string) => void | Callback for SDK log messages. |
onSubProcessLogMessage | (message: string) => void | Callback for shard process log messages. |
disableLogs / disableErrorLogs | boolean | Silence SDK logs, or only the error logs. |
disableSubProcessLogs / disableSubProcessErrorLogs | boolean | Silence shard stdout and stderr mirroring. |
const fnlb = new FNLB({
clusterName: 'MyAwesomeCluster',
channel: 'stable',
onLogMessage: (message) => console.log(message)
});start() (StartConfig)
| Option | Type | Description |
|---|---|---|
apiToken / token | string | Your FNLB_... API token or FNLBOA2AT_... OAuth2 access token. |
categories | string[] | Category ids whose bots may run. Omit (or pass []) to allow bots from any category. |
bots | string[] | Exact bot ids to run. |
numberOfShards | number | Number of shards (subprocesses) to spawn. Defaults to 1. |
botsPerShard | number | Maximum number of bots each shard may run. Defaults to 1. |
overrideCategoryConfig | object | Per-category and per-bot config overrides. See Override category settings. |
channel | 'stable' | 'beta' | 'dev' | Release channel for this run. |
logLevel | 'INFO' | 'DEBUG' | Log verbosity. The LogLevel enum is exported by the SDK. |
hideUsernames / hideEmails | boolean | Redact usernames or emails in shard logs. |
extraEnv | Record<string, string> | Extra environment variables passed to each shard process. |
How bot selection works
categories and bots are include lists:
- Categories only — runs every bot in those categories, plus bots that have no category.
- Bot ids only — runs only the listed bots.
- Both — runs the union of the two selections.
- Neither — runs your full bot pool.
Invalid ids are ignored. Multiple shards can share the same bots list: the gateway assigns each id to exactly one shard, so no bot runs twice. Omit categories (or pass []) when you only want specific bot ids from any category; when categories is set, bots without a category are still included.
Running multiple bots on one shard
botsPerShard sets the maximum number of bots that can run inside a single shard (subprocess).
await fnlb.start({
apiToken: 'your-api-token',
categories: ['your-category-id'],
botsPerShard: 10
});Scaling with multiple shards
Each shard is its own subprocess. Multiply shards by bots-per-shard to get your capacity ceiling.
await fnlb.start({
apiToken: 'your-api-token',
categories: ['your-category-id'],
numberOfShards: 2,
botsPerShard: 10 // up to 20 bots total
});Launching bots across multiple categories
await fnlb.start({
apiToken: 'your-api-token',
categories: ['category-id-1', 'category-id-2'],
numberOfShards: 2,
botsPerShard: 10
});Starting specific bots by id
await fnlb.start({
apiToken: 'your-api-token',
bots: ['bot-id-1', 'bot-id-2'],
botsPerShard: 2
});Bot ids come from a bot's page at app.fnlb.net/bots under About this bot (FNLB ID), or from GET /bots. You can combine bots with categories; the selection is the union of both lists. Multiple shards may share the same bots list without starting a bot twice.
Override category settings at start
overrideCategoryConfig applies config overrides when the shards start. Overrides are scoped and merged in this order (later wins):
- The category's own FNLB configuration.
all— applies to every bot.categories— entries matching the bot's category.bots— entries matching the bot's id.
import FNLB from 'fnlb';
import { CategoryConfigPartyPrivacy } from '@fnlb-project/shared/types';
const fnlb = new FNLB();
await fnlb.start({
apiToken: 'your-api-token',
categories: ['category-id-1'],
overrideCategoryConfig: {
all: { acceptFriendRequests: false },
categories: {
'category-id-1': { privacy: CategoryConfigPartyPrivacy.Private }
},
bots: {
'bot-id-1': { statusText: ['Special bot'] }
}
}
});Overrides stay applied even if the category config is changed in the dashboard while the bots are running. Every key is in the category config reference.
Update your selection without restarting
After start(), you can change which categories or bots the running shards may use without calling stop().
| Method | Description |
|---|---|
addCategories(ids, overrides?) | Add category ids to the include list. |
addBots(ids, overrides?) | Add bot ids to the include list. |
setCategories(ids?) | Replace the category include list. Call with no arguments to make it unrestricted. |
setBots(ids?, overrides?) | Replace the bot include list. Call with no arguments to make it unrestricted. |
removeCategories(ids) | Remove category ids from the include list. |
removeBots(ids) | Remove bot ids from the include list. |
getCategories() | Current category include list (undefined means unrestricted). |
getBots() | Current bot include list (undefined means unrestricted). |
The optional overrides argument is a partial category config applied to the ids you pass, using the same merge rules as overrideCategoryConfig. The keys are listed in category config.
import FNLB from 'fnlb';
const fnlb = new FNLB();
await fnlb.start({
apiToken: 'your-api-token',
categories: ['category-id-1'],
botsPerShard: 10
});
// Add a category with a per-category override.
fnlb.addCategories(['category-id-2'], { acceptFriendRequests: false });
// Add a specific bot with its own override.
fnlb.addBots(['bot-id-1'], { acceptFriendRequests: false });
// Replace the selection entirely.
fnlb.setBots(['bot-id-1', 'bot-id-2']);
fnlb.setCategories(); // back to unrestricted
// Shrink the selection again.
fnlb.removeCategories(['category-id-1']);
fnlb.removeBots(['bot-id-1']);Behavior details:
undefinedmeans unrestricted for that dimension.add*on an unrestricted dimension starts an include list (it narrows from "all").add*on an existing list unions and de-duplicates.set*replaces the list; passingundefinedor omitting the argument makes it unrestricted.remove*on an unrestricted dimension is a no-op.- Removing until the list is empty returns that dimension to unrestricted.
- Overrides passed to
add*orset*are stored per id and removed withremove*(or whenset*drops that id). - These methods throw if no shards are running.
Stopping bots
await fnlb.stop();This stops every shard and every bot managed by this instance.
Graceful shutdown
Stop the shards before the process exits so bots disconnect cleanly.
const shutdown = async () => {
await fnlb.stop();
process.exit(0);
};
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);Naming your cluster
const fnlb = new FNLB({ clusterName: 'MyAwesomeCluster' });
await fnlb.start({ apiToken: 'your-api-token' });The cluster name appears in logs and on the gateway.
Auto-restart every hour
Restarting periodically picks up SDK updates and clears long-lived connections.
async function startFNLB() {
await fnlb.start({
apiToken: 'your-api-token',
categories: ['your-category-id'],
numberOfShards: 1,
botsPerShard: 5
});
}
async function restartFNLB() {
await fnlb.stop();
await startFNLB();
}
await startFNLB();
setInterval(restartFNLB, 3_600_000);Events, bots, and party meta
The SDK guides walk through this in full. After start(), the client emits events from the local shards and can call the bots they are running.
import FNLB, { Events } from 'fnlb';
const fnlb = new FNLB();
fnlb.on(Events.Ready, (bot) => {
console.log(bot.displayName);
});
fnlb.on(Events.PartyMemberMessage, async (message) => {
if (message.content === '!ping') await message.reply('pong');
});
await fnlb.start({
token: 'your-api-token',
categories: ['your-category-id']
});
const bot = await fnlb.bots.fetch('bot-id');
await bot.party.meta.get();
await bot.party.meta.set('Default:CustomMatchKey_s', 'my-key');
await bot.runCommand('outfit', ['renegade']);fnlb.bots.cache is the bots running in this process. bot.party.me.meta reads and writes that bot's member meta. bot.friends(), bot.whisper(), party.invite, party.kick, party.promote, party.leave, and fnlb.fetchShardInfo() use the same local bridge.
HTTP API
See HTTP client for routes, scopes, and when to use the API instead of local IPC. fnlb.api calls https://api.fnlb.net/v1 with the start() token. Override the origin with apiBaseUrl.
const bots = await fnlb.api.bots.list();
const user = await fnlb.api.user.get();
await fnlb.api.bots.commands.run(bots[0].id, { command: 'emote', args: ['wave'] });
await fnlb.api.categories.list();
await fnlb.api.presets.list();Cloud commands.run works for bots that are not running in this process. Local bot.runCommand talks to the shard directly.
Handling errors
try {
await fnlb.start({ apiToken: 'your-api-token', categories: ['your-category-id'] });
} catch (error) {
console.error('Failed to start FNLB:', error);
process.exit(1);
}Verifying your cluster
Check the shards the gateway currently knows about with GET /shards. If nothing appears, confirm that your token has the required scopes and that the machine can reach api.fnlb.net and gateway.fnlb.net.
From this portal
- Look up override keys in category config.
- Continue with the SDK guides for events, parties, chat, commands, and
fnlb.api. - Open the SDK Reference or download /sdk.json.
- Run a cluster from env vars with self-hosting FNLB.
- Inspect your running shards with GET /shards.
- See what changed in the changelog.
- Join Discord
Get help, support, or chat with other developers