{{ message }}
feat(js-sdk): include creditsUsed in v2 search() response (#3351)#3366
Open
MukundaKatta wants to merge 1 commit intofirecrawl:mainfrom
Open
feat(js-sdk): include creditsUsed in v2 search() response (#3351)#3366MukundaKatta wants to merge 1 commit intofirecrawl:mainfrom
MukundaKatta wants to merge 1 commit intofirecrawl:mainfrom
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Fixes #3351.
The
/v2/searchAPI returnscreditsUsedin the top-level response, but the JS SDK'ssearch()method was dropping it when buildingSearchData. 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 optionalcreditsUsed?: numberto theSearchDatainterface (same shape as existingcreditsUsed?fields onDocument,CrawlJob,ScrapeData, etc.)src/v2/methods/search.ts— forwardres.data.creditsUsedonto the returnedSearchDatawhen the API includes itsrc/__tests__/unit/v2/search.test.ts— new unit tests covering:creditsUsedis surfacedcreditsUsedleaves the fieldundefined(no breaking type change)creditsUsed: 0is 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 cleanlySummary by cubic
Expose
creditsUsedfrom/v2/searchin the JS SDK’ssearch()response so apps can track per-request credit usage. Alignssearch()with other v2 methods likescrapeandcrawl.creditsUsed?: numbertoSearchData(no breaking change).res.data.creditsUsedto the returnedSearchData, including when it is0.Written for commit 1874ae6. Summary will update on new commits.