{"id":16377,"library":"header-generator","title":"HTTP Header Generator","description":"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.","status":"active","version":"2.1.82","language":"javascript","source_language":"en","source_url":"https://github.com/apify/fingerprint-suite","tags":["javascript","typescript"],"install":[{"cmd":"npm install header-generator","lang":"bash","label":"npm"},{"cmd":"yarn add header-generator","lang":"bash","label":"yarn"},{"cmd":"pnpm add header-generator","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"While CommonJS `require` works in Node.js, modern TypeScript/ESM projects should use named imports. This is the primary class for generating headers.","wrong":"const { HeaderGenerator } = require('header-generator');","symbol":"HeaderGenerator","correct":"import { HeaderGenerator } from 'header-generator';"},{"note":"PRESETS is a named export object containing predefined configurations for common browser/OS combinations.","wrong":"import PRESETS from 'header-generator';","symbol":"PRESETS","correct":"import { PRESETS } from 'header-generator';"},{"note":"This is a TypeScript type definition for the options object passed to the HeaderGenerator constructor or getHeaders method. It should be imported as a type.","wrong":"import { HeaderGeneratorOptions } from 'header-generator';","symbol":"HeaderGeneratorOptions","correct":"import type { HeaderGeneratorOptions } from 'header-generator';"}],"quickstart":{"code":"import { HeaderGenerator, PRESETS } from 'header-generator';\n\n(async () => {\n    // Initialize the generator with default constraints\n    const headerGenerator = new HeaderGenerator({\n        browsers: [\n            { name: 'chrome', minVersion: 90 },\n            { name: 'firefox', minVersion: 80 },\n            'safari'\n        ],\n        devices: ['desktop', 'mobile'],\n        operatingSystems: ['windows', 'macos', 'linux', 'android', 'ios'],\n        locales: ['en-US', 'en', 'de-DE'],\n        httpVersion: '2'\n    });\n\n    // Generate headers based on the global options (randomly selected from possibilities)\n    const headers1 = headerGenerator.getHeaders();\n    console.log('Generated Headers (default options):', headers1);\n\n    // Generate headers with specific, overriding options for a single call\n    const headers2 = headerGenerator.getHeaders({\n        operatingSystems: ['linux'],\n        devices: ['desktop'],\n        locales: ['es-ES']\n    });\n    console.log('Generated Headers (Linux desktop, Spanish locale):', headers2);\n\n    // Initialize with a preset for a common configuration\n    const modernWindowsChromeGenerator = new HeaderGenerator(PRESETS.MODERN_WINDOWS_CHROME);\n    const headers3 = modernWindowsChromeChromeGenerator.getHeaders();\n    console.log('Generated Headers (Modern Windows Chrome preset):', headers3);\n\n    // Headers are generated randomly, subsequent calls yield different results\n    const headers4 = headerGenerator.getHeaders();\n    console.log('Another set of Headers (randomized):', headers4);\n})();","lang":"typescript","description":"Demonstrates initializing HeaderGenerator with options, generating headers with global and specific constraints, and using predefined PRESETS."},"warnings":[{"fix":"Always consult the main `fingerprint-suite` repository for overarching architecture and updated guidelines regarding `header-generator`'s usage.","message":"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.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Store the result of `getHeaders()` if you need to reuse the same set of headers. Do not assume repeated calls will yield identical results without explicit caching.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Manually add request-specific headers to the generated object before sending an HTTP request. For example, `const fullHeaders = { ...generatedHeaders, Host: 'example.com', 'Content-Length': body.length };`","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure the `httpVersion` option matches the actual HTTP version used by your client to avoid sending malformed or incorrect headers.","message":"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).","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Ensure `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');`.","cause":"Incorrect import path, missing `header-generator` from `package.json`, or trying to use CommonJS `require` in an ESM-only context without proper configuration.","error":"Error: Cannot find module 'header-generator'"},{"fix":"Verify that `headerGenerator = new HeaderGenerator(...)` was called successfully before attempting to use `getHeaders()`.","cause":"The `headerGenerator` variable was not correctly initialized as an instance of `HeaderGenerator` or was reassigned.","error":"TypeError: headerGenerator.getHeaders is not a function"},{"fix":"Consult the `HeaderGeneratorOptions` interface and documentation to ensure all options passed are valid. Remove or correct any misspelled or unsupported options.","cause":"Attempting to pass an unrecognized option to the HeaderGenerator constructor or `getHeaders` method when using TypeScript, indicating a type mismatch or invalid configuration.","error":"Property 'someUnknownOption' does not exist on type 'HeaderGeneratorOptions'"}],"ecosystem":"npm"}