Skip to content

feat: add SELF_HOSTED_DOMAIN config for custom hostname support (fix #2964) - #3015

Closed
gambletan wants to merge 5 commits into
firecrawl:mainfrom
gambletan:fix/2964-self-hosted-domain
Closed

gambletan wants to merge 5 commits into
firecrawl:mainfrom
gambletan:fix/2964-self-hosted-domain

Conversation

@gambletan

@gambletan gambletan commented Mar 8, 2026 •

Copy link
Copy Markdown

This fix addresses Issue #2964 by adding a new environment variable
SELF_HOSTED_DOMAIN that allows self-hosted users to specify a custom
hostname when running behind a reverse proxy.

Previously, the API always returned URLs with the internal hostname
(localhost:3002) regardless of the external domain being used.

Changes:

  • Add SELF_HOSTED_DOMAIN config option in config.ts
  • Create getHostname() helper function in host-utils.ts
  • Update v1 crawl, batch-scrape, and crawl-status controllers
  • Update v2 crawl, batch-scrape, and crawl-status controllers
  • Document the new environment variable in .env.example

Usage:

Set the environment variable SELF_HOSTED_DOMAIN to your custom domain:

SELF_HOSTED_DOMAIN=firecrawl.example.com

This will cause the API to return URLs like:

https://firecrawl.example.com/v1/crawl/...

instead of:

http://localhost:3002/v1/crawl/...

Summary by cubic

Add custom hostname support for self‑hosted deployments via SELF_HOSTED_DOMAIN, so API responses return external URLs instead of internal hosts (fixes #2964). Also improves scraping reliability, PDF handling, credits reporting, and adds custom headers support in the JS SDK.

  • New Features

    • SELF_HOSTED_DOMAIN env var with getHostname helper; v1/v2 controllers now build URLs using your external domain. Set SELF_HOSTED_DOMAIN=firecrawl.example.com.
    • JS SDK: added headers option to include custom HTTP headers in all requests (e.g., Cloudflare Access, custom auth).
  • Bug Fixes

    • Fire Engine: insert a 500ms wait after click actions to stabilize dynamic UIs.
    • Agent/extract status: return creditsUsed while processing by querying EXTRACT_V3_BETA_URL, falling back to 0 if unavailable.
    • PDF engine: detect HTML responses from .pdf URLs and fall back to other engines; added unit tests for HTML/PDF sniffing.

Written for commit d477a16. Summary will update on new commits.

gambletan and others added 5 commits March 8, 2026 17:56
Add 'headers' option to FirecrawlClientOptions to allow users to specify
custom HTTP headers that will be included in all API requests.

This enables self-hosted Firecrawl users to:
- Pass Cloudflare Access headers (CF-Access-Client-Id, CF-Access-Client-Secret)
- Use custom authorization schemes
- Work with reverse proxy authentication
- Add custom tracking headers

Closes firecrawl#2814
This fix addresses Issue firecrawl#2809 where clicking elements in dynamic dropdowns
fails with 'Element not found' because the next action executes before
the DOM has updated.

The fix automatically inserts a 500ms wait after each click action to allow
dynamic content (dropdowns, modals, etc.) to render before the next action
is executed.

Without this fix, users had to manually add a screenshot action between
click actions to work around the timing issue.
…irecrawl#2891)

- When agent job is in progress (status=processing), creditsUsed was not
  being returned in the response
- Now attempts to fetch real-time credits from EXTRACT_V3_BETA_URL service
- Falls back to 0 if external service is unavailable
- Applied same fix to extract-status.ts for agent kind requests
When a URL has .pdf extension but returns HTML content (webpage with
embedded PDF), the PDF engine now detects this and throws
EngineUnsuccessfulError to fall back to other engines like Playwright.

This fixes Issue firecrawl#839 where URLs like https://www.gamweb.com/assets/files/lsk.pdf
return HTML instead of a raw PDF file.

Changes:
- Add isHtmlBuffer() function to detect HTML content in pdfUtils.ts
- Check for HTML content in PDF engine before attempting PDF parsing
- Add unit tests for isHtmlBuffer()
This fix addresses Issue firecrawl#2964 by adding a new environment variable
SELF_HOSTED_DOMAIN that allows self-hosted users to specify a custom
hostname when running behind a reverse proxy.

Previously, the API always returned URLs with the internal hostname
(localhost:3002) regardless of the external domain being used.

Changes:
- Add SELF_HOSTED_DOMAIN config option
- Create getHostname() helper function in host-utils.ts
- Update v1 and v2 controllers to use custom hostname when set
- Document the new environment variable in .env.example

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6 issues found across 17 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="apps/api/src/config.ts">

<violation number="1" location="apps/api/src/config.ts:38">
P2: `SELF_HOSTED_DOMAIN` is accepted as an arbitrary string, but it is later used as a hostname in URL construction, allowing malformed values to generate invalid API URLs.</violation>
</file>

<file name="apps/api/src/controllers/v2/extract-status.ts">

<violation number="1" location="apps/api/src/controllers/v2/extract-status.ts:60">
P2: New outbound `fetch` in status request path lacks explicit timeout/abort, allowing upstream slowness to delay status responses.</violation>
</file>

<file name="apps/api/src/controllers/v1/crawl-status.ts">

<violation number="1" location="apps/api/src/controllers/v1/crawl-status.ts:278">
P2: `next` URL construction assumes host-only output from `getHostname`, but helper/config allow full URL strings, which can produce malformed pagination links.</violation>
</file>

<file name="apps/api/src/scraper/scrapeURL/engines/pdf/index.ts">

<violation number="1" location="apps/api/src/scraper/scrapeURL/engines/pdf/index.ts:77">
P1: HTML short-circuit now masks PDF anti-bot failures as generic engine misses, bypassing PDFAntibotError retry/prefetch handling.</violation>
</file>

<file name="apps/api/src/lib/host-utils.ts">

<violation number="1" location="apps/api/src/lib/host-utils.ts:10">
P2: Empty `SELF_HOSTED_DOMAIN` is treated as a valid hostname, which can generate malformed absolute URLs.</violation>
</file>

<file name="apps/api/src/scraper/scrapeURL/engines/fire-engine/index.ts">

<violation number="1" location="apps/api/src/scraper/scrapeURL/engines/fire-engine/index.ts:308">
P2: Chrome-CDP runtime estimate is stale: newly inserted 500ms post-click waits are not included in `fireEngineMaxReasonableTime`, underestimating expected duration for click-heavy actions.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

// downloaded content isn't a valid PDF
// Check if it's HTML (webpage with embedded PDF) - fall back to other engines
if (isHtmlBuffer(file.buffer)) {
throw new EngineUnsuccessfulError("pdf");

@cubic-dev-ai cubic-dev-ai Bot Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: HTML short-circuit now masks PDF anti-bot failures as generic engine misses, bypassing PDFAntibotError retry/prefetch handling.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/src/scraper/scrapeURL/engines/pdf/index.ts, line 77:

<comment>HTML short-circuit now masks PDF anti-bot failures as generic engine misses, bypassing PDFAntibotError retry/prefetch handling.</comment>

<file context>
@@ -72,6 +72,10 @@ export async function scrapePDF(meta: Meta): Promise<EngineScrapeResult> {
         // downloaded content isn't a valid PDF
+        // Check if it's HTML (webpage with embedded PDF) - fall back to other engines
+        if (isHtmlBuffer(file.buffer)) {
+          throw new EngineUnsuccessfulError("pdf");
+        }
         if (meta.pdfPrefetch === undefined) {
</file context>
Fix with Cubic

Comment thread apps/api/src/config.ts
FIRECRAWL_APP_HOST: z.string().default("firecrawl-app-service"),
FIRECRAWL_APP_PORT: z.string().default("3002"),
FIRECRAWL_APP_SCHEME: z.string().default("http"),
SELF_HOSTED_DOMAIN: z.string().optional(),

@cubic-dev-ai cubic-dev-ai Bot Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: SELF_HOSTED_DOMAIN is accepted as an arbitrary string, but it is later used as a hostname in URL construction, allowing malformed values to generate invalid API URLs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/src/config.ts, line 38:

<comment>`SELF_HOSTED_DOMAIN` is accepted as an arbitrary string, but it is later used as a hostname in URL construction, allowing malformed values to generate invalid API URLs.</comment>

<file context>
@@ -35,6 +35,7 @@ const configSchema = z.object({
   FIRECRAWL_APP_HOST: z.string().default("firecrawl-app-service"),
   FIRECRAWL_APP_PORT: z.string().default("3002"),
   FIRECRAWL_APP_SCHEME: z.string().default("http"),
+  SELF_HOSTED_DOMAIN: z.string().optional(),
   LOGGING_LEVEL: z.string().optional(),
 
</file context>
Suggested change
SELF_HOSTED_DOMAIN: z.string().optional(),
SELF_HOSTED_DOMAIN: z
.string()
.trim()
.regex(
/^(?!.*:\/\/)(?!\s*$)([a-zA-Z0-9.-]+|\[[0-9a-fA-F:]+\])(?::\d{1,5})?$/,
"SELF_HOSTED_DOMAIN must be a hostname (or host:port) without scheme/path",
)
.optional(),
Fix with Cubic

let creditsUsed: number | undefined = agent?.credits_cost;
if (!agent && config.EXTRACT_V3_BETA_URL) {
try {
const creditsRequest = await fetch(

@cubic-dev-ai cubic-dev-ai Bot Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: New outbound fetch in status request path lacks explicit timeout/abort, allowing upstream slowness to delay status responses.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/src/controllers/v2/extract-status.ts, line 60:

<comment>New outbound `fetch` in status request path lacks explicit timeout/abort, allowing upstream slowness to delay status responses.</comment>

<file context>
@@ -53,6 +53,37 @@ export async function extractStatusController(
+      let creditsUsed: number | undefined = agent?.credits_cost;
+      if (!agent && config.EXTRACT_V3_BETA_URL) {
+        try {
+          const creditsRequest = await fetch(
+            config.EXTRACT_V3_BETA_URL +
+              "/v2/extract/" +
</file context>
Fix with Cubic

(outputBulkA.total ?? 0) > start + iteratedOver ||
outputBulkA.status !== "completed"
? `${req.protocol}://${req.get("host")}/v1/${isBatch ? "batch/scrape" : "crawl"}/${req.params.jobId}?skip=${start + iteratedOver}${req.query.limit ? `&limit=${req.query.limit}` : ""}`
? `${req.protocol}://${getHostname(req)}/v1/${isBatch ? "batch/scrape" : "crawl"}/${req.params.jobId}?skip=${start + iteratedOver}${req.query.limit ? `&limit=${req.query.limit}` : ""}`

@cubic-dev-ai cubic-dev-ai Bot Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: next URL construction assumes host-only output from getHostname, but helper/config allow full URL strings, which can produce malformed pagination links.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/src/controllers/v1/crawl-status.ts, line 278:

<comment>`next` URL construction assumes host-only output from `getHostname`, but helper/config allow full URL strings, which can produce malformed pagination links.</comment>

<file context>
@@ -274,7 +275,7 @@ export async function crawlStatusController(
       (outputBulkA.total ?? 0) > start + iteratedOver ||
       outputBulkA.status !== "completed"
-        ? `${req.protocol}://${req.get("host")}/v1/${isBatch ? "batch/scrape" : "crawl"}/${req.params.jobId}?skip=${start + iteratedOver}${req.query.limit ? `&limit=${req.query.limit}` : ""}`
+        ? `${req.protocol}://${getHostname(req)}/v1/${isBatch ? "batch/scrape" : "crawl"}/${req.params.jobId}?skip=${start + iteratedOver}${req.query.limit ? `&limit=${req.query.limit}` : ""}`
         : undefined,
   };
</file context>
Fix with Cubic

* This is useful when running behind a reverse proxy.
*/
export function getHostname(req: Request): string {
return config.SELF_HOSTED_DOMAIN ?? req.get("host") ?? "localhost";

@cubic-dev-ai cubic-dev-ai Bot Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Empty SELF_HOSTED_DOMAIN is treated as a valid hostname, which can generate malformed absolute URLs.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/src/lib/host-utils.ts, line 10:

<comment>Empty `SELF_HOSTED_DOMAIN` is treated as a valid hostname, which can generate malformed absolute URLs.</comment>

<file context>
@@ -0,0 +1,11 @@
+ * This is useful when running behind a reverse proxy.
+ */
+export function getHostname(req: Request): string {
+  return config.SELF_HOSTED_DOMAIN ?? req.get("host") ?? "localhost";
+}
</file context>
Fix with Cubic

}),
// Include specified actions, with automatic waits after clicks to allow
// dynamic content (dropdowns, modals, etc.) to render
...insertWaitsAfterClicks(

@cubic-dev-ai cubic-dev-ai Bot Mar 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Chrome-CDP runtime estimate is stale: newly inserted 500ms post-click waits are not included in fireEngineMaxReasonableTime, underestimating expected duration for click-heavy actions.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/api/src/scraper/scrapeURL/engines/fire-engine/index.ts, line 308:

<comment>Chrome-CDP runtime estimate is stale: newly inserted 500ms post-click waits are not included in `fireEngineMaxReasonableTime`, underestimating expected duration for click-heavy actions.</comment>

<file context>
@@ -281,11 +303,14 @@ export async function scrapeURLWithFireEngineChromeCDP(
-      }),
+      // Include specified actions, with automatic waits after clicks to allow
+      // dynamic content (dropdowns, modals, etc.) to render
+      ...insertWaitsAfterClicks(
+        (meta.options.actions ?? []).map(action => {
+          const { metadata: _, ...rest } = action as InternalAction;
</file context>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Self-Host] custom hostname

3 participants