API Reference
This page provides detailed documentation for the tlsx API.
Core Functions
generateCertificate
Generates a certificate for one or multiple domains.
ts
async function generateCertificate(options: CertificateOptions): Promise<Certificate>Parameters:
options: Configuration options for the certificate generationdomain: Primary domain for the certificatedomains: Array of additional domainsrootCA: Root CA certificate and private keyvalidityDays: Number of days the certificate is validcountryName: Two-letter country codestateName: State or province namelocalityName: City or locality nameorganizationName: Organization namecommonName: Common name for the certificatealtNameIPs: Array of IP addresses to include as SANsaltNameURIs: Array of URIs to include as SANssubjectAltNames: Array of custom SANsbasicConstraints: Basic constraints extensionkeyUsage: Key usage extensionextKeyUsage: Extended key usage extensionverbose: Enable verbose logging
Returns:
Certificate: Object containing certificate, private key, and validity dates
Example:
ts
const cert = await generateCertificate({
domain: 'example.local',
domains: ['api.example.local'],
rootCA: existingCA,
validityDays: 365,
})createRootCA
Creates a new Certificate Authority (CA) certificate.
ts
async function createRootCA(options?: CAOptions): Promise<Certificate>Parameters:
options: Configuration options for the CA certificate generationkeySize: RSA key size (default: 2048)validityYears: Number of years the CA is valid (default: 10)countryName: Two-letter country codestateName: State or province namelocalityName: City or locality nameorganization: Organization nameorganizationalUnit: Organizational unit namecommonName: Common name for the CAextraAttributes: Additional subject attributesverbose: Enable verbose logging
Returns:
Certificate: Object containing CA certificate, private key, and validity dates
Example:
ts
const rootCA = await createRootCA({
commonName: 'Local Development Root CA',
organization: 'My Organization',
validityYears: 10,
})addCertToSystemTrustStoreAndSaveCert
Adds a certificate to the system trust store and saves it to a file.
ts
async function addCertToSystemTrustStoreAndSaveCert(
cert: Cert,
caCert: string,
options?: TlsOption
): Promise<CertPath>Parameters:
cert: Certificate and private keycaCert: CA certificateoptions: Configuration optionsbasePath: Base path for storing certificatescertPath: Path for the certificate filekeyPath: Path for the private key filecaCertPath: Path for the CA certificate fileverbose: Enable verbose logging
Returns:
CertPath: Path to the stored certificate
Example:
ts
const certPath = await addCertToSystemTrustStoreAndSaveCert(
cert,
rootCA.certificate,
{ verbose: true }
)storeCertificate
Stores a certificate and private key to the filesystem.
ts
function storeCertificate(cert: Cert, options?: TlsOption): CertPathParameters:
cert: Certificate and private keyoptions: Configuration optionsbasePath: Base path for storing certificatescertPath: Path for the certificate filekeyPath: Path for the private key fileverbose: Enable verbose logging
Returns:
CertPath: Path to the stored certificate
Example:
ts
const certPath = storeCertificate(cert, {
basePath: '/custom/path',
certPath: 'my-cert.crt',
keyPath: 'my-key.key',
})storeCACertificate
Stores a CA certificate to the filesystem.
ts
function storeCACertificate(caCert: string, options?: TlsOption): CertPathParameters:
caCert: CA certificateoptions: Configuration optionsbasePath: Base path for storing certificatescaCertPath: Path for the CA certificate fileverbose: Enable verbose logging
Returns:
CertPath: Path to the stored CA certificate
Example:
ts
const caCertPath = storeCACertificate(rootCA.certificate, {
basePath: '/custom/path',
caCertPath: 'my-ca.crt',
})Utility Functions
generateRandomSerial
Generates a random serial number for a certificate.
ts
function generateRandomSerial(verbose?: boolean): RandomSerialNumberParameters:
verbose: Enable verbose logging
Returns:
RandomSerialNumber: A random serial number
Example:
ts
const serialNumber = generateRandomSerial()calculateValidityDates
Calculates the validity dates for a certificate.
ts
function calculateValidityDates(options: {
validityDays?: number
validityYears?: number
notBeforeDays?: number
verbose?: boolean
}): { notBefore: Date, notAfter: Date }Parameters:
options: Configuration optionsvalidityDays: Number of days the certificate is validvalidityYears: Number of years the certificate is validnotBeforeDays: Number of days before the current date to start validityverbose: Enable verbose logging
Returns:
- Object containing
notBeforeandnotAfterdates
Example:
ts
const { notBefore, notAfter } = calculateValidityDates({
validityDays: 365,
notBeforeDays: 1,
})Types
Certificate
ts
interface Certificate {
certificate: string
privateKey: string
notBefore: Date
notAfter: Date
}CertificateOptions
ts
interface CertificateOptions {
domain?: string
domains?: string[]
rootCA: {
certificate: string
privateKey: string
}
validityDays?: number
countryName?: string
stateName?: string
localityName?: string
organizationName?: string
commonName?: string
certificateAttributes?: Array<{
shortName: string
value: string
}>
basicConstraints?: {
cA?: boolean
critical?: boolean
}
keyUsage?: {
digitalSignature?: boolean
nonRepudiation?: boolean
keyEncipherment?: boolean
dataEncipherment?: boolean
keyAgreement?: boolean
keyCertSign?: boolean
cRLSign?: boolean
encipherOnly?: boolean
decipherOnly?: boolean
critical?: boolean
}
extKeyUsage?: {
serverAuth?: boolean
clientAuth?: boolean
codeSigning?: boolean
emailProtection?: boolean
timeStamping?: boolean
critical?: boolean
}
altNameIPs?: string[]
altNameURIs?: string[]
subjectAltNames?: SubjectAltName[]
isCA?: boolean
verbose?: boolean
}CAOptions
ts
interface CAOptions {
keySize?: number
validityYears?: number
countryName?: string
stateName?: string
localityName?: string
organization?: string
organizationalUnit?: string
commonName?: string
extraAttributes?: Array<{
shortName: string
value: string
}>
verbose?: boolean
}TlsOption
ts
interface TlsOption {
basePath?: string
certPath?: string
keyPath?: string
caCertPath?: string
verbose?: boolean
}Constants and Config
config
Default configuration values used by tlsx.
ts
const config: {
basePath: string
caCertPath: string
certPath: string
keyPath: string
domain: string
hostCertCN: string
commonName: string
countryName: string
stateName: string
localityName: string
organizationName: string
validityDays: number
altNameIPs: string[]
altNameURIs: string[]
}