Skip to content

Authentications

thibaut severac edited this page Oct 27, 2026 · 4 revisions

Authentications

crowdsec-client supports three authentication methods:

  1. Machine ID + password for a watcher.
  2. API key for a bouncer.
  3. TLS certificates (mutual TLS) for a watcher or a bouncer.

login() must be called before using a client. A bouncer login() only checks connectivity (the API key is sent on every request); a watcher login() obtains a bearer token.

See the CrowdSec Local API authentication documentation for the server-side setup.

Machine ID and password (watcher)

This method is reserved for watchers.

Create the machine credentials with:

cscli machines add node-watcher -p myPassword -f ./node_credential.yaml

-f writes the generated credentials to a file; keep that file safe.

import { WatcherClient } from 'crowdsec-client';

const watcherClient = new WatcherClient({
    url: 'https://crowdsec.lan',
    auth: {
        machineID: 'node-watcher',
        password: 'myPassword',
        // optional, default true: renew the token before it expires
        autoRenew: true
    }
});

await watcherClient.login();

The token returned by the LAPI is valid for one hour. With autoRenew enabled (the default), the client renews it five minutes before expiry.

API key (bouncer)

This method is reserved for bouncers.

Create an API key with:

cscli bouncers add MyBouncerName

Keep the generated key: it is only displayed once.

import { BouncerClient } from 'crowdsec-client';

const client = new BouncerClient({
    url: 'https://crowdsec.lan',
    auth: {
        apiKey: 'myApiKey'
    }
});

await client.login();

TLS authentication

TLS authentication lets a watcher or a bouncer authenticate without a manual login. See the CrowdSec TLS auth documentation for the server and certificate setup.

import fs from 'node:fs';
import path from 'node:path';
import { BouncerClient, WatcherClient } from 'crowdsec-client';

// watcher
const watcher = new WatcherClient({
    url: 'https://crowdsec.lan',
    auth: {
        cert: fs.readFileSync('agent.pem'),
        key: fs.readFileSync('agent-key.pem'),
        ca: fs.readFileSync('inter.pem')
    }
});

await watcher.login();

// bouncer
const client = new BouncerClient({
    url: 'https://crowdsec.lan',
    auth: {
        cert: fs.readFileSync(path.join(TLSPath, 'bouncer.pem')),
        key: fs.readFileSync(path.join(TLSPath, 'bouncer-key.pem')),
        ca: fs.readFileSync(path.join(TLSPath, 'inter.pem'))
    }
});

await client.login();

Self-signed certificates

strictSSL defaults to true. Only disable it for development, or when your certificate cannot be validated:

const client = new BouncerClient({
    url: 'https://crowdsec.lan',
    auth: { apiKey: 'myApiKey' },
    strictSSL: false
});

Security notes

  • Use a strong password, or let cscli generate one. Avoid hardcoding credentials in the examples above.
  • Do not commit credential files or environment files to a repository.
  • Prefer TLS authentication, or a secret manager, for production deployments.

Clone this wiki locally