HTTP Header Generator
header-generator is a core component of the Apify Fingerprint Suite, a toolkit designed to generate realistic, browser-like HTTP headers for web scraping and anti-fingerprinting purposes. Currently at version 2.1.82, the package receives frequent, automated updates primarily for its underlying data models, ensuring the generated headers remain current and effective against sophisticated bot detection. While its standalone GitHub repository is marked as deprecated, the package itself is actively maintained and developed as an integral part of the larger fingerprint-suite project. It allows developers to programmatically create HTTP headers that mimic various browsers, operating systems, devices, and locales, helping scrapers blend in more effectively with legitimate user traffic. Its key differentiator is the focus on realism and configurability based on real-world browser data.
Common errors
-
Error: Cannot find module 'header-generator'
cause Incorrect import path, missing `header-generator` from `package.json`, or trying to use CommonJS `require` in an ESM-only context without proper configuration.fixEnsure `header-generator` is listed in `dependencies` and installed (`npm install header-generator`). Use `import { HeaderGenerator } from 'header-generator';` for ESM/TypeScript projects. For older Node.js or CommonJS, `const { HeaderGenerator } = require('header-generator');`. -
TypeError: headerGenerator.getHeaders is not a function
cause The `headerGenerator` variable was not correctly initialized as an instance of `HeaderGenerator` or was reassigned.fixVerify that `headerGenerator = new HeaderGenerator(...)` was called successfully before attempting to use `getHeaders()`. -
Property 'someUnknownOption' does not exist on type 'HeaderGeneratorOptions'
cause Attempting to pass an unrecognized option to the HeaderGenerator constructor or `getHeaders` method when using TypeScript, indicating a type mismatch or invalid configuration.fixConsult the `HeaderGeneratorOptions` interface and documentation to ensure all options passed are valid. Remove or correct any misspelled or unsupported options.
Warnings
- gotcha The standalone 'apify/header-generator' GitHub repository is marked as deprecated. While the `header-generator` npm package remains active and receives updates (primarily for its underlying data models), it is now managed as part of the broader Apify `fingerprint-suite` project. Users are encouraged to refer to the `fingerprint-suite` documentation for the latest context and best practices.
- gotcha The `getHeaders()` method always generates a *random* set of headers from the possible options. Calling it multiple times with the same parameters will likely produce different header sets, which is by design for anti-fingerprinting, but might be unexpected if deterministic output is desired.
- gotcha The generated headers do not include request-dependent headers such as `Host`, `Content-Length`, `Cookie`, or `If-None-Match`. These headers are specific to the individual HTTP request and must be added by the user's HTTP client or application logic.
- gotcha The `httpVersion` option in `HeaderGeneratorOptions` significantly impacts the structure and content of the generated headers, as HTTP/1 and HTTP/2 requests use different header sets (e.g., HTTP/2 uses `:method`, `:authority`, `:scheme`, `:path` pseudo-headers).
Install
-
npm install header-generator -
yarn add header-generator -
pnpm add header-generator
Imports
- HeaderGenerator
const { HeaderGenerator } = require('header-generator');import { HeaderGenerator } from 'header-generator'; - PRESETS
import PRESETS from 'header-generator';
import { PRESETS } from 'header-generator'; - HeaderGeneratorOptions
import { HeaderGeneratorOptions } from 'header-generator';import type { HeaderGeneratorOptions } from 'header-generator';
Quickstart
import { HeaderGenerator, PRESETS } from 'header-generator';
(async () => {
// Initialize the generator with default constraints
const headerGenerator = new HeaderGenerator({
browsers: [
{ name: 'chrome', minVersion: 90 },
{ name: 'firefox', minVersion: 80 },
'safari'
],
devices: ['desktop', 'mobile'],
operatingSystems: ['windows', 'macos', 'linux', 'android', 'ios'],
locales: ['en-US', 'en', 'de-DE'],
httpVersion: '2'
});
// Generate headers based on the global options (randomly selected from possibilities)
const headers1 = headerGenerator.getHeaders();
console.log('Generated Headers (default options):', headers1);
// Generate headers with specific, overriding options for a single call
const headers2 = headerGenerator.getHeaders({
operatingSystems: ['linux'],
devices: ['desktop'],
locales: ['es-ES']
});
console.log('Generated Headers (Linux desktop, Spanish locale):', headers2);
// Initialize with a preset for a common configuration
const modernWindowsChromeGenerator = new HeaderGenerator(PRESETS.MODERN_WINDOWS_CHROME);
const headers3 = modernWindowsChromeChromeGenerator.getHeaders();
console.log('Generated Headers (Modern Windows Chrome preset):', headers3);
// Headers are generated randomly, subsequent calls yield different results
const headers4 = headerGenerator.getHeaders();
console.log('Another set of Headers (randomized):', headers4);
})();