feat(js-sdk): include creditsUsed in v2 search() response (#3351) by MukundaKatta · Pull Request #3366 · firecrawl/firecrawl · GitHub
Skip to content

feat(js-sdk): include creditsUsed in v2 search() response (#3351)#3366

Open
MukundaKatta wants to merge 1 commit intofirecrawl:mainfrom
MukundaKatta:feat/3351-search-credits-used
Open

feat(js-sdk): include creditsUsed in v2 search() response (#3351)#3366
MukundaKatta wants to merge 1 commit intofirecrawl:mainfrom
MukundaKatta:feat/3351-search-credits-used

Conversation

@MukundaKatta
Copy link
Copy Markdown

@MukundaKatta MukundaKatta commented Apr 14, 2026

Summary

Fixes #3351.

The /v2/search API returns creditsUsed in the top-level response, but the JS SDK's search() method was dropping it when building SearchData. Callers who wanted to track per-request credit consumption through the SDK had no accurate way to do so — they were stuck estimating from the pricing page.

This PR forwards the field through, matching what scrape, crawl, and the other v2 methods already expose.

Changes

  • src/v2/types.ts — add optional creditsUsed?: number to the SearchData interface (same shape as existing creditsUsed? fields on Document, CrawlJob, ScrapeData, etc.)
  • src/v2/methods/search.ts — forward res.data.creditsUsed onto the returned SearchData when the API includes it
  • src/__tests__/unit/v2/search.test.ts — new unit tests covering:
    • populated creditsUsed is surfaced
    • missing creditsUsed leaves the field undefined (no breaking type change)
    • creditsUsed: 0 is still forwarded (no truthy-check bug)

Kept the field optional rather than defaulted-to-0 so the SDK's output reflects exactly what the API sent — and so this isn't a breaking type change for existing consumers.

Test plan

  • pnpm test:unit — all 47 tests pass (3 new + 44 existing)
  • pnpm build — DTS + CJS + ESM all emit cleanly

Summary by cubic

Expose creditsUsed from /v2/search in the JS SDK’s search() response so apps can track per-request credit usage. Aligns search() with other v2 methods like scrape and crawl.

  • Bug Fixes
    • Added optional creditsUsed?: number to SearchData (no breaking change).
    • Forwarded res.data.creditsUsed to the returned SearchData, including when it is 0.
    • Added unit tests for present, absent, and zero cases.

Written for commit 1874ae6. Summary will update on new commits.

…3351)

The /v2/search API returns `creditsUsed` in the top-level response, but
the JS SDK's `search()` method dropped it when building `SearchData`.
That made it impossible for consumers to track per-request credit usage
through the SDK — they had to fall back to hardcoded estimates.

- Add optional `creditsUsed?: number` to `SearchData` (matches the
  existing pattern on `Document`, `CrawlJob`, `ScrapeData`, etc.).
- Forward `res.data.creditsUsed` from the HTTP response in
  `v2/methods/search.ts` when present.
- Add unit tests covering populated, absent, and `0` cases.

Fixes firecrawl#3351
Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

No issues found across 3 files

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.

[Feat] Include creditsUsed in JS SDK search() response

1 participant