const pems = await selfsigned.generate(null, { keyType: 'rsa', // key type: 'rsa' or 'ec' (default: 'rsa') keySize: 2048, // the size for the private key in bits (default: 2048, RSA only) curve: 'P-256', // elliptic curve: 'P-256', 'P-384', or 'P-521' (default: 'P-256', EC only) notBeforeDate: new Date(), // start of certificate validity (default: now) notAfterDate: new Date('2026-01-01'), // end of certificate validity (default: notBeforeDate + 365 days) algorithm: 'sha256', // sign the certificate with specified algorithm (default: 'sha1') extensions: [{ name: 'basicConstraints', cA: true }], // certificate extensions array clientCertificate: true, // generate client cert (default: false) - can also be an options object ca: { key: '...', cert: '...'
Setting Custom Validity Period
Use notBeforeDate and notAfterDate to control certificate validity:
// Using date-fnsconst { addDays, addYears } = require('date-fns');const pems = await selfsigned.generate(null, { notBeforeDate: new Date(), notAfterDate: addDays(new Date(), 30) // Valid for 30 days});// Or with vanilla JSconst notBefore = new Date();const notAfter = new Date(notBefore);notAfter.setFullYear(notAfter.getFullYear() + 2); // Valid for 2 yearsconst pems = await selfsigned.
Supported Algorithms
sha1 (default)
sha256
sha384
sha512
Custom Extensions
You can customize certificate extensions using the extensions option. This is useful for adding Subject Alternative Names (SANs) with IPv6 addresses, custom key usage, and more.
{ name: 'basicConstraints', cA: true, // is this a CA certificate? pathLenConstraint: 0, // max depth of valid cert chain (optional) critical: true // mark as critical extension}
keyUsage
{ name: 'keyUsage', digitalSignature: true, nonRepudiation: true, keyEncipherment: true, dataEncipherment: true, keyAgreement: true, keyCertSign: true, // for CA certificates cRLSign: true, // for CA certificates encipherOnly: true, decipherOnly: true, critical: true}
By default, selfsigned generates RSA keys. You can generate certificates using elliptic curve cryptography instead, which provides equivalent security with smaller key sizes and faster operations.
You can encrypt the private key with a passphrase using AES-256-CBC:
const pems = await selfsigned.generate(null, { passphrase: 'my-secret-passphrase'});// The private key will be in encrypted PKCS#8 format:// -----BEGIN ENCRYPTED PRIVATE KEY-----// ...// -----END ENCRYPTED PRIVATE KEY-----
To use the encrypted key, provide the passphrase:
const crypto = require('crypto');// Decrypt the keyconst privateKey = crypto.createPrivateKey({ key: pems.private, passphrase: 'my-secret-passphrase'});// Or use directly with HTTPS serverconst https = require('https');https.createServer({ key: pems.private, passphrase: 'my-secret-passphrase', cert: pems.cert}, app).listen(443);
Signing with a CA
You can generate certificates signed by an existing Certificate Authority instead of self-signed certificates. This is useful for development environments where you want browsers to trust your certificates.