Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

[codex] Add v2 research proxy and SDKs (#3794)

Gergő Móriczd47a4f304694
diff --git a/apps/api/src/__tests__/snips/v2/lib.ts b/apps/api/src/__tests__/snips/v2/lib.ts
index 0ed0e701e..4092620e2 100644
--- a/apps/api/src/__tests__/snips/v2/lib.ts
+++ b/apps/api/src/__tests__/snips/v2/lib.ts
@@ -638,6 +638,20 @@ export async function searchWithFailure(
return raw.body;
}
+export async function researchRaw(
+ path: string,
+ query: Record<string, string | number | boolean | string[]> | undefined,
+ identity?: Identity,
+) {
+ const req = request(TEST_API_URL)
+ .get(path)
+ .set("Content-Type", "application/json");
+ if (identity) {
+ req.set("Authorization", `Bearer ${identity.apiKey}`);
+ }
+ return query ? req.query(query) : req;
+}
+
export async function searchRawFull(
body: SearchRequestInput,
identity: Identity,
diff --git a/apps/api/src/__tests__/snips/v2/research.test.ts b/apps/api/src/__tests__/snips/v2/research.test.ts
new file mode 100644
index 000000000..d6c585b33
--- /dev/null
+++ b/apps/api/src/__tests__/snips/v2/research.test.ts
@@ -0,0 +1,139 @@
+import { config } from "../../../config";
+import { describeIf, TEST_PRODUCTION } from "../lib";
+import { creditUsage, idmux, researchRaw } from "./lib";
+
+const HAS_RESEARCH = !!config.RESEARCH_PROXY_URL;
+const KEYLESS_ENABLED =
+ process.env.KEYLESS_REQUESTS_PER_DAY !== undefined &&
+ process.env.KEYLESS_CREDITS_PER_DAY !== undefined;
+
+const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
+const sleepForBilling = () => sleep(40000);
+
+describeIf(HAS_RESEARCH)("Research API", () => {
+ it("serves paper search from the canonical mount", async () => {
+ const identity = await idmux({
+ name: "research/canonical paper search",
+ credits: 100,
+ });
+
+ const res = await researchRaw(
+ "/v2/search/research/papers",
+ { query: "retrieval augmented generation", k: 2 },
+ identity,
+ );
+
+ expect(res.statusCode).toBe(200);
+ expect(res.body.success).toBe(true);
+ expect(Array.isArray(res.body.results)).toBe(true);
+ expect(res.body.results.length).toBeGreaterThan(0);
+ expect(res.body.results[0].paperId).toBeDefined();
+ expect(res.body.results[0].paper_id).toBeUndefined();
+ }, 120000);
+
+ it("keeps the legacy research mount working", async () => {
+ const identity = await idmux({
+ name: "research/legacy paper search",
+ credits: 100,
+ });
+
+ const res = await researchRaw(
+ "/v2/research/papers",
+ { query: "diffusion models", k: 1 },
+ identity,
+ );
+
+ expect(res.statusCode).toBe(200);
+ expect(res.body.success).toBe(true);
+ expect(Array.isArray(res.body.results)).toBe(true);
+ expect(res.body.results[0].paperId).toBeDefined();
+ expect(res.body.results[0].paper_id).toBe(res.body.results[0].paperId);
+ }, 120000);
+
+ it("rejects invalid endpoint-specific query params", async () => {
+ const identity = await idmux({
+ name: "research/invalid params",
+ credits: 100,
+ });
+
+ const res = await researchRaw(
+ "/v2/search/research/papers",
+ { query: "rag", magic: "true" } as any,
+ identity,
+ );
+
+ expect(res.statusCode).toBe(400);
+ expect(res.body.success).toBe(false);
+ });
+
+ it("rejects paper inspect k without read query", async () => {
+ const identity = await idmux({
+ name: "research/inspect rejects k",
+ credits: 100,
+ });
+
+ const res = await researchRaw(
+ "/v2/search/research/papers/1706.03762",
+ { k: 1 },
+ identity,
+ );
+
+ expect(res.statusCode).toBe(400);
+ expect(res.body.success).toBe(false);
+ });
+
+ describeIf(KEYLESS_ENABLED)("keyless research", () => {
+ it("permits keyless access on the canonical research index", async () => {
+ const res = await researchRaw("/v2/search/research/papers", {
+ query: "transformers",
+ k: 1,
+ });
+
+ expect(res.statusCode).not.toBe(401);
+ }, 120000);
+ });
+
+ describeIf(TEST_PRODUCTION)("research billing", () => {
+ it("bills read-paper as one scrape-like credit", async () => {
+ const identity = await idmux({
+ name: "research/bills read paper",
+ credits: 100,
+ });
+ const before = (await creditUsage(identity)).remainingCredits;
+
+ const res = await researchRaw(
+ "/v2/search/research/papers/1706.03762",
+ { query: "attention", k: 1 },
+ identity,
+ );
+ expect(res.statusCode).toBe(200);
+ expect(res.body.success).toBe(true);
+
+ await sleepForBilling();
+ const after = (await creditUsage(identity)).remainingCredits;
+ expect(before - after).toBe(1);
+ }, 180000);
+
+ it("bills search-like endpoints by returned result count", async () => {
+ const identity = await idmux({
+ name: "research/bills search papers",
+ credits: 100,
+ });
+ const before = (await creditUsage(identity)).remainingCredits;
+
+ const res = await researchRaw(
+ "/v2/search/research/papers",
+ { query: "graph neural networks", k: 11 },
+ identity,
+ );
+ expect(res.statusCode).toBe(200);
+ expect(res.body.success).toBe(true);
+ expect(res.body.results.length).toBeGreaterThan(0);
+ const expectedCredits = Math.ceil(res.body.results.length / 10) * 2;
+
+ await sleepForBilling();
+ const after = (await creditUsage(identity)).remainingCredits;
+ expect(before - after).toBe(expectedCredits);
+ }, 180000);
+ });
+});
diff --git a/apps/api/src/controllers/auth.ts b/apps/api/src/controllers/auth.ts
index 81eaeb915..d7a50c6c0 100644
--- a/apps/api/src/controllers/auth.ts
+++ b/apps/api/src/controllers/auth.ts
@@ -561,9 +561,11 @@ async function handleKeylessAuth(
const modeLabel =
mode === RateLimiterMode.Search
? "search"
- : mode === RateLimiterMode.BrowserExecute
- ? "interact"
- : "scrape";
+ : mode === RateLimiterMode.Research
+ ? "research"
+ : mode === RateLimiterMode.BrowserExecute
+ ? "interact"
+ : "scrape";
let result: Awaited<ReturnType<typeof consumeKeylessRequest>>;
try {
@@ -831,7 +833,8 @@ async function supaAuthenticateUser(
mode === RateLimiterMode.Crawl ||
mode === RateLimiterMode.CrawlStatus ||
mode === RateLimiterMode.Extract ||
- mode === RateLimiterMode.Search)
+ mode === RateLimiterMode.Search ||
+ mode === RateLimiterMode.Research)
) {
return {
success: true,
diff --git a/apps/api/src/controllers/v2/research-proxy.ts b/apps/api/src/controllers/v2/research-proxy.ts
index 2ea86ea5b..c8f6a78a8 100644
--- a/apps/api/src/controllers/v2/research-proxy.ts
+++ b/apps/api/src/controllers/v2/research-proxy.ts
@@ -1,12 +1,28 @@
-import { NextFunction, Response } from "express";
+import express, { Request, Response } from "express";
import { Agent, fetch } from "undici";
+import { z } from "zod";
+import { v7 as uuidv7 } from "uuid";
import { config } from "../../config";
-import { logger } from "../../lib/logger";
-import { RequestWithAuth, RequestWithMaybeACUC } from "../v1/types";
+import { logger as rootLogger } from "../../lib/logger";
+import { chargeKeylessCredits } from "../../lib/keyless";
+import { billTeam } from "../../services/billing/credit_billing";
+import { getSearchForcedKind } from "../../lib/zdr-helpers";
+import {
+ logRequest,
+ logResearchEndpoint,
+} from "../../services/logging/log_job";
+import type {
+ ResearchRequestKind,
+ ResearchTableName,
+} from "../../services/logging/log_job";
+import type { RequestWithAuth } from "../v1/types";
+import { wrap } from "../../routes/shared";
const TIMEOUT_MS = 120_000;
+const SEARCH_CREDITS_PER_TEN_RESULTS = 2;
+const ZDR_SEARCH_CREDITS_PER_TEN_RESULTS = 10;
-const FORWARDED_REQUEST_HEADERS = ["content-type", "accept", "x-request-id"];
+const FORWARDED_REQUEST_HEADERS = ["accept", "x-request-id"];
const FORWARDED_RESPONSE_HEADERS = ["content-type", "x-request-id"];
const dispatcher = new Agent({
@@ -15,36 +31,190 @@ const dispatcher = new Agent({
bodyTimeout: TIMEOUT_MS,
});
-export function researchFlagMiddleware(
- req: RequestWithMaybeACUC<any, any, any>,
- res: Response,
- next: NextFunction,
+const multiString = z
+ .union([z.string(), z.array(z.string())])
+ .optional()
+ .transform(value => {
+ if (!value) return undefined;
+ return Array.isArray(value) ? value : [value];
+ });
+
+const kSchema = (max: number) =>
+ z.coerce.number().int().positive().max(max).optional();
+
+const dateSchema = z
+ .string()
+ .regex(/^\d{4}-\d{2}-\d{2}$/)
+ .optional();
+
+const commonQuery = {
+ origin: z.string().optional(),
+ integration: z.string().optional(),
+};
+
+const searchPapersSchema = z.strictObject({
+ query: z.string().min(1),
+ k: kSchema(500),
+ authors: multiString,
+ categories: multiString,
+ from: dateSchema,
+ to: dateSchema,
+ ...commonQuery,
+});
+
+const paperSchema = z
+ .strictObject({
+ query: z.string().min(1).optional(),
+ k: kSchema(50),
+ ...commonQuery,
+ })
+ .refine(value => value.query !== undefined || value.k === undefined, {
+ message: "k is only valid when query is present",
+ path: ["k"],
+ });
+
+const similarPapersSchema = z.strictObject({
+ intent: z.string().min(1),
+ mode: z.enum(["similar", "citers", "references"]).optional(),
+ k: kSchema(500),
+ rerank: z
+ .enum(["true", "false"])
+ .optional()
+ .transform(value => (value === undefined ? undefined : value === "true")),
+ anchor: multiString,
+ ...commonQuery,
+});
+
+const githubSearchSchema = z.strictObject({
+ query: z.string().min(1),
+ k: kSchema(100),
+ ...commonQuery,
+});
+
+type ResearchEndpointConfig = {
+ kind: ResearchRequestKind;
+ table: ResearchTableName;
+ action: string;
+ targetHint: (
+ params: Record<string, any>,
+ req: RequestWithAuth<any, any, any>,
+ ) => string;
+ upstreamPath: (
+ params: Record<string, any>,
+ req: RequestWithAuth<any, any, any>,
+ ) => string;
+ billAs: "scrape" | "search";
+};
+
+type ResearchController = (req: Request, res: Response) => Promise<any>;
+type ResearchQueryParams = Record<string, any> & {
+ origin?: string;
+ integration?: string;
+};
+
+const LEGACY_SNAKE_CASE_ALIASES: Record<string, string> = {
+ paperId: "paper_id",
+ primaryId: "primary_id",
+ createdDate: "created_date",
+ updateDate: "update_date",
+ articleRank: "article_rank",
+ seedOverlap: "seed_overlap",
+ poolSize: "pool_size",
+ resultType: "result_type",
+ pageType: "page_type",
+ segmentCount: "segment_count",
+ readmeUrl: "readme_url",
+ contentMd: "content_md",
+};
+
+function addLegacySnakeCaseAliases<T>(value: T): T {
+ if (Array.isArray(value)) {
+ return value.map(addLegacySnakeCaseAliases) as T;
+ }
+
+ if (!value || typeof value !== "object") {
+ return value;
+ }
+
+ const source = value as Record<string, unknown>;
+ const object: Record<string, unknown> = {};
+
+ for (const [key, item] of Object.entries(source)) {
+ object[key] = addLegacySnakeCaseAliases(item);
+ }
+
+ for (const [camelKey, snakeKey] of Object.entries(
+ LEGACY_SNAKE_CASE_ALIASES,
+ )) {
+ if (Object.prototype.hasOwnProperty.call(source, camelKey)) {
+ object[snakeKey] = object[camelKey];
+ }
+ }
+
+ return object as T;
+}
+
+function appendQuery(
+ url: URL,
+ params: Record<string, unknown>,
+ allowed: string[],
) {
- if (!req.acuc?.flags?.researchBeta) {
- return res.status(403).json({ success: false, error: "Forbidden" });
+ for (const key of allowed) {
+ const value = params[key];
+ if (Array.isArray(value)) {
+ for (const item of value) {
+ if (item !== undefined && item !== null) {
+ url.searchParams.append(key, String(item));
+ }
+ }
+ } else if (value !== undefined && value !== null) {
+ url.searchParams.append(key, String(value));
+ }
}
- next();
}
-export async function researchProxyController(
+function resultCount(body: any): number {
+ return Array.isArray(body?.results) ? body.results.length : 0;
+}
+
+function creditsFor(
+ config: ResearchEndpointConfig,
+ body: any,
req: RequestWithAuth<any, any, any>,
+) {
+ if (config.billAs === "scrape") return 1;
+ const forcedKind = getSearchForcedKind(req.acuc?.flags);
+ const perTen =
+ forcedKind === "zdr"
+ ? ZDR_SEARCH_CREDITS_PER_TEN_RESULTS
+ : SEARCH_CREDITS_PER_TEN_RESULTS;
+ return Math.ceil(resultCount(body) / 10) * perTen;
+}
+
+function researchError(
res: Response,
-): Promise<void> {
+ status: number,
+ error: string,
+ details?: unknown,
+) {
+ return res.status(status).json({
+ success: false,
+ error,
+ ...(details === undefined ? {} : { details }),
+ });
+}
+
+async function fetchResearchUpstream(
+ req: RequestWithAuth<any, any, any>,
+ path: string,
+ params: Record<string, unknown>,
+ queryKeys: string[],
+) {
const base = config.RESEARCH_PROXY_URL;
- if (!base) {
- res.status(404).end();
- return;
- }
+ if (!base) return null;
- const fullPath = req.baseUrl + req.path || "/";
- const url = new URL(base.replace(/\/+$/, "") + fullPath);
- for (const [k, v] of Object.entries(req.query)) {
- if (Array.isArray(v)) {
- v.forEach(vv => url.searchParams.append(k, String(vv)));
- } else if (v !== undefined && v !== null) {
- url.searchParams.append(k, String(v));
- }
- }
+ const url = new URL(base.replace(/\/+$/, "") + path);
+ appendQuery(url, params, queryKeys);
const headers: Record<string, string> = {};
for (const h of FORWARDED_REQUEST_HEADERS) {
@@ -53,38 +223,253 @@ export async function researchProxyController(
}
headers["firecrawl-team-id"] = req.auth.team_id;
- const init: Parameters<typeof fetch>[1] = {
- method: req.method,
+ return fetch(url, {
+ method: "GET",
headers,
signal: AbortSignal.timeout(TIMEOUT_MS),
dispatcher,
- };
+ });
+}
- if (req.method !== "GET" && req.method !== "HEAD") {
- if (
- req.body &&
- typeof req.body === "object" &&
- Object.keys(req.body).length > 0
- ) {
- init.body = JSON.stringify(req.body);
- headers["content-type"] = "application/json";
- }
- }
+function createResearchController(
+ schema: z.ZodTypeAny,
+ queryKeys: string[],
+ endpoint: ResearchEndpointConfig,
+ options: { legacy?: boolean } = {},
+): ResearchController {
+ return async (req, res: Response) => {
+ const authedReq = req as RequestWithAuth<any, any, any>;
+ const started = Date.now();
+ const jobId = uuidv7();
+ const logger = rootLogger.child({
+ module: "api/v2/research",
+ method: endpoint.action,
+ jobId,
+ teamId: authedReq.auth.team_id,
+ });
- try {
- const upstream = await fetch(url, init);
- res.status(upstream.status);
- for (const h of FORWARDED_RESPONSE_HEADERS) {
- const v = upstream.headers.get(h);
- if (v) res.setHeader(h, v);
+ const parsed = schema.safeParse(req.query);
+ if (!parsed.success) {
+ logger.warn("Invalid research query", { error: parsed.error.issues });
+ return researchError(
+ res,
+ 400,
+ "Invalid query parameters",
+ parsed.error.issues,
+ );
}
- res.send(Buffer.from(await upstream.arrayBuffer()));
- } catch (err: unknown) {
- if (err instanceof DOMException && err.name === "TimeoutError") {
- res.status(504).end();
- return;
+
+ const params = parsed.data as ResearchQueryParams;
+ const targetHint = endpoint.targetHint(params, authedReq);
+ await logRequest({
+ id: jobId,
+ kind: endpoint.kind,
+ api_version: "v2",
+ team_id: authedReq.auth.team_id,
+ origin: params.origin ?? "api",
+ integration: params.integration ?? null,
+ target_hint: targetHint,
+ zeroDataRetention: false,
+ api_key_id: authedReq.acuc?.api_key_id ?? null,
+ });
+
+ let statusCode = 500;
+ let responseBody: any = null;
+ let error: string | undefined;
+ let credits = 0;
+
+ try {
+ const upstream = await fetchResearchUpstream(
+ authedReq,
+ endpoint.upstreamPath(params, authedReq),
+ params,
+ queryKeys,
+ );
+ if (!upstream) {
+ statusCode = 404;
+ error = "Research service is not configured";
+ return res.status(404).end();
+ }
+
+ statusCode = upstream.status;
+ for (const h of FORWARDED_RESPONSE_HEADERS) {
+ const value = upstream.headers.get(h);
+ if (value) res.setHeader(h, value);
+ }
+
+ const text = await upstream.text();
+ try {
+ responseBody = text ? JSON.parse(text) : null;
+ } catch {
+ responseBody = text;
+ }
+
+ if (upstream.ok) {
+ credits = creditsFor(endpoint, responseBody, authedReq);
+ if (credits > 0) {
+ billTeam(
+ authedReq.auth.team_id,
+ authedReq.acuc?.sub_id ?? undefined,
+ credits,
+ authedReq.acuc?.api_key_id ?? null,
+ {
+ endpoint: endpoint.billAs === "scrape" ? "scrape" : "search",
+ jobId,
+ },
+ ).catch(billingError => {
+ logger.error("Failed to bill research request", {
+ error: billingError,
+ credits,
+ });
+ });
+ chargeKeylessCredits(authedReq.auth.team_id, credits).catch(() => {});
+ }
+ } else {
+ error =
+ typeof responseBody === "object" && responseBody !== null
+ ? (responseBody.detail ?? responseBody.error ?? responseBody.title)
+ : undefined;
+ }
+
+ if (responseBody === null || typeof responseBody === "string") {
+ return res.status(statusCode).send(responseBody ?? "");
+ }
+ const response =
+ options.legacy && upstream.ok
+ ? addLegacySnakeCaseAliases(responseBody)
+ : responseBody;
+ return res.status(statusCode).json(response);
+ } catch (err: unknown) {
+ if (err instanceof DOMException && err.name === "TimeoutError") {
+ statusCode = 504;
+ error = "Research service timed out";
+ return res.status(504).end();
+ }
+ statusCode = 502;
+ error = "Research proxy error";
+ logger.error("Research proxy error", { error: err });
+ return res.status(502).end();
+ } finally {
+ const timeTaken = (Date.now() - started) / 1000;
+ logResearchEndpoint({
+ table: endpoint.table,
+ id: jobId,
+ request_id: jobId,
+ team_id: authedReq.auth.team_id,
+ target: targetHint,
+ options: params,
+ response: responseBody,
+ num_results: resultCount(responseBody),
+ time_taken: timeTaken,
+ credits_cost: statusCode >= 200 && statusCode < 300 ? credits : 0,
+ is_successful: statusCode >= 200 && statusCode < 300,
+ error,
+ zeroDataRetention: false,
+ }).catch(logError => {
+ logger.warn("Research endpoint log failed", { error: logError });
+ });
}
- logger.error("Research proxy error", { error: err });
- res.status(502).end();
+ };
+}
+
+export function createResearchRouter(options: { legacy?: boolean } = {}) {
+ const router = express.Router();
+
+ if (options.legacy) {
+ router.use((req, _res, next) => {
+ rootLogger.warn("Legacy research endpoint used", {
+ module: "api/v2/research",
+ teamId: (req as RequestWithAuth<any, any, any>).auth?.team_id,
+ method: req.method,
+ path: req.originalUrl,
+ requestId: req.headers["x-request-id"],
+ });
+ next();
+ });
}
+
+ router.get(
+ "/papers",
+ wrap(
+ createResearchController(
+ searchPapersSchema,
+ ["query", "k", "authors", "categories", "from", "to"],
+ {
+ kind: "research_paper_search",
+ table: "research_paper_searches",
+ action: "searchPapers",
+ targetHint: params => String(params.query),
+ upstreamPath: () => "/v2/research/papers",
+ billAs: "search",
+ },
+ options,
+ ),
+ ),
+ );
+
+ router.get(
+ "/papers/:id/similar",
+ wrap(
+ createResearchController(
+ similarPapersSchema,
+ ["intent", "mode", "k", "rerank", "anchor"],
+ {
+ kind: "research_related_papers",
+ table: "research_related_papers",
+ action: "similarPapers",
+ targetHint: (params, req) =>
+ `${req.params.id}: ${String(params.intent)}`,
+ upstreamPath: (_params, req) =>
+ `/v2/research/papers/${encodeURIComponent(req.params.id)}/similar`,
+ billAs: "search",
+ },
+ options,
+ ),
+ ),
+ );
+
+ router.get(
+ "/papers/:id",
+ wrap(async (req: Request, res: Response) => {
+ const authedReq = req as RequestWithAuth<any, any, any>;
+ const parsed = paperSchema.safeParse(req.query);
+ const isRead = parsed.success && parsed.data.query !== undefined;
+ const controller = createResearchController(
+ paperSchema,
+ ["query", "k"],
+ {
+ kind: isRead ? "research_paper_read" : "research_paper_inspect",
+ table: isRead ? "research_paper_reads" : "research_paper_inspects",
+ action: isRead ? "readPaper" : "inspectPaper",
+ targetHint: (_params, request) => request.params.id,
+ upstreamPath: (_params, request) =>
+ `/v2/research/papers/${encodeURIComponent(request.params.id)}`,
+ billAs: "scrape",
+ },
+ options,
+ );
+ return controller(authedReq, res);
+ }),
+ );
+
+ router.get(
+ "/github",
+ wrap(
+ createResearchController(
+ githubSearchSchema,
+ ["query", "k"],
+ {
+ kind: "research_github_search",
+ table: "research_github_searches",
+ action: "searchGithub",
+ targetHint: params => String(params.query),
+ upstreamPath: () => "/v2/research/github",
+ billAs: "search",
+ },
+ options,
+ ),
+ ),
+ );
+
+ return router;
}
diff --git a/apps/api/src/db/schema/public.ts b/apps/api/src/db/schema/public.ts
index f73752a35..f03241d65 100644
--- a/apps/api/src/db/schema/public.ts
+++ b/apps/api/src/db/schema/public.ts
@@ -162,6 +162,42 @@ export const deep_researches = pgTable("deep_researches", {
options: jsonb("options"),
});
+const researchEndpointTable = (name: string) =>
+ pgTable(name, {
+ id: uuid("id").notNull(),
+ request_id: uuid("request_id").notNull(),
+ target: text("target").notNull(),
+ team_id: uuid("team_id").notNull(),
+ options: jsonb("options"),
+ response: jsonb("response"),
+ num_results: integer("num_results").notNull(),
+ time_taken: num("time_taken").notNull(),
+ credits_cost: integer("credits_cost").notNull(),
+ is_successful: boolean("is_successful").notNull(),
+ error: text("error"),
+ created_at: ts("created_at").notNull().defaultNow(),
+ });
+
+export const research_paper_searches = researchEndpointTable(
+ "research_paper_searches",
+);
+
+export const research_paper_inspects = researchEndpointTable(
+ "research_paper_inspects",
+);
+
+export const research_paper_reads = researchEndpointTable(
+ "research_paper_reads",
+);
+
+export const research_related_papers = researchEndpointTable(
+ "research_related_papers",
+);
+
+export const research_github_searches = researchEndpointTable(
+ "research_github_searches",
+);
+
export const deterministic_json_scripts = pgTable(
"deterministic_json_scripts",
{
diff --git a/apps/api/src/routes/v2.ts b/apps/api/src/routes/v2.ts
index 9df0a6cbf..b1cd15866 100644
--- a/apps/api/src/routes/v2.ts
+++ b/apps/api/src/routes/v2.ts
@@ -62,10 +62,7 @@ import {
} from "../controllers/v2/browser";
import { activityController } from "../controllers/v1/activity";
import { supportProxyController } from "../controllers/v2/support-proxy";
-import {
- researchFlagMiddleware,
- researchProxyController,
-} from "../controllers/v2/research-proxy";
+import { createResearchRouter } from "../controllers/v2/research-proxy";
import {
scrapeInteractController,
scrapeStopInteractiveBrowserController,
@@ -593,11 +590,16 @@ v2Router.post(
);
if (config.RESEARCH_PROXY_URL) {
- v2Router.all(
- "/research/*",
+ v2Router.use(
+ "/search/research",
+ authMiddleware(RateLimiterMode.Research, { allowKeyless: true }),
+ createResearchRouter(),
+ );
+
+ v2Router.use(
+ "/research",
authMiddleware(RateLimiterMode.Research),
- researchFlagMiddleware,
- wrap(researchProxyController),
+ createResearchRouter({ legacy: true }),
);
}
diff --git a/apps/api/src/services/logging/log_job.ts b/apps/api/src/services/logging/log_job.ts
index d9a3eab41..20852deef 100644
--- a/apps/api/src/services/logging/log_job.ts
+++ b/apps/api/src/services/logging/log_job.ts
@@ -45,6 +45,11 @@ const tableMap: Record<string, PgTable> = {
crawls: schema.crawls,
batch_scrapes: schema.batch_scrapes,
searches: schema.searches,
+ research_paper_searches: schema.research_paper_searches,
+ research_paper_inspects: schema.research_paper_inspects,
+ research_paper_reads: schema.research_paper_reads,
+ research_related_papers: schema.research_related_papers,
+ research_github_searches: schema.research_github_searches,
extracts: schema.extracts,
maps: schema.maps,
llmstxts: schema.llmstxts,
@@ -166,7 +171,12 @@ type LoggedRequest = {
| "parse"
| "agent"
| "browser"
- | "interact";
+ | "interact"
+ | "research_paper_search"
+ | "research_paper_inspect"
+ | "research_paper_read"
+ | "research_related_papers"
+ | "research_github_search";
api_version: string;
team_id: string;
origin?: string;
@@ -490,6 +500,76 @@ export async function logSearch(search: LoggedSearch, force: boolean = false) {
}
}
+export type ResearchRequestKind =
+ | "research_paper_search"
+ | "research_paper_inspect"
+ | "research_paper_read"
+ | "research_related_papers"
+ | "research_github_search";
+
+export type ResearchTableName =
+ | "research_paper_searches"
+ | "research_paper_inspects"
+ | "research_paper_reads"
+ | "research_related_papers"
+ | "research_github_searches";
+
+type LoggedResearchEndpoint = {
+ table: ResearchTableName;
+ id: string;
+ request_id: string;
+ target: string;
+ team_id: string;
+ options: any;
+ response: any;
+ num_results: number;
+ time_taken: number;
+ credits_cost: number;
+ is_successful: boolean;
+ error?: string;
+ zeroDataRetention: boolean;
+};
+
+export async function logResearchEndpoint(
+ research: LoggedResearchEndpoint,
+ force: boolean = false,
+) {
+ const logger = _logger.child({
+ module: "log_job",
+ method: "logResearchEndpoint",
+ researchId: research.id,
+ requestId: research.request_id,
+ teamId: research.team_id,
+ zeroDataRetention: research.zeroDataRetention,
+ });
+
+ await robustInsert(
+ research.table,
+ {
+ id: research.id,
+ request_id: research.request_id,
+ target: research.zeroDataRetention
+ ? "<redacted due to zero data retention>"
+ : (sanitizeString(research.target) ?? ""),
+ team_id:
+ keylessTeamUuid(research.team_id) ??
+ (research.team_id === "preview" ||
+ research.team_id?.startsWith("preview_")
+ ? previewTeamId
+ : research.team_id),
+ options: research.zeroDataRetention ? null : research.options,
+ response: research.zeroDataRetention ? null : research.response,
+ num_results: research.num_results,
+ time_taken: research.time_taken,
+ credits_cost: research.credits_cost,
+ is_successful: research.is_successful,
+ error: research.zeroDataRetention ? null : (research.error ?? null),
+ },
+ force,
+ logger,
+ );
+}
+
export type LoggedExtract = {
id: string;
request_id: string;
diff --git a/apps/api/src/services/rate-limiter.ts b/apps/api/src/services/rate-limiter.ts
index a71163866..c8ea8cc04 100644
--- a/apps/api/src/services/rate-limiter.ts
+++ b/apps/api/src/services/rate-limiter.ts
@@ -32,7 +32,7 @@ const fallbackRateLimits: AuthCreditUsageChunk["rate_limits"] = {
account: 1000,
supportAsk: 3,
supportDocsSearch: 3,
- research: 20,
+ research: 100,
};
export function getRateLimiter(
diff --git a/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs b/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs
index fde84b672..1b5b4b005 100644
--- a/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs
+++ b/apps/dot-net-sdk/Firecrawl/FirecrawlClient.cs
@@ -461,6 +461,113 @@ public class FirecrawlClient
return response.Data ?? throw new FirecrawlException("Search response contained no data");
}
+ /// <summary>
+ /// Searches research papers.
+ /// </summary>
+ public async Task<SearchPapersResponse> SearchPapersAsync(
+ string query,
+ SearchPapersOptions? options = null,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(query);
+
+ return await _http.GetAsync<SearchPapersResponse>(
+ "/v2/search/research/papers" + BuildResearchQuery(new Dictionary<string, object?>
+ {
+ ["query"] = query,
+ ["origin"] = SdkOrigin,
+ ["k"] = options?.K,
+ ["authors"] = options?.Authors,
+ ["categories"] = options?.Categories,
+ ["from"] = options?.From,
+ ["to"] = options?.To
+ }),
+ cancellationToken);
+ }
+
+ /// <summary>
+ /// Fetches paper metadata.
+ /// </summary>
+ public async Task<PaperMetadataResponse> InspectPaperAsync(
+ string paperId,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(paperId);
+
+ return await _http.GetAsync<PaperMetadataResponse>(
+ $"/v2/search/research/papers/{Uri.EscapeDataString(paperId)}",
+ cancellationToken);
+ }
+
+ /// <summary>
+ /// Fetches paper metadata and query-guided passages.
+ /// </summary>
+ public async Task<ReadPaperResponse> ReadPaperAsync(
+ string paperId,
+ string query,
+ ReadPaperOptions? options = null,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(paperId);
+ ArgumentNullException.ThrowIfNull(query);
+
+ return await _http.GetAsync<ReadPaperResponse>(
+ $"/v2/search/research/papers/{Uri.EscapeDataString(paperId)}"
+ + BuildResearchQuery(new Dictionary<string, object?>
+ {
+ ["query"] = query,
+ ["origin"] = SdkOrigin,
+ ["k"] = options?.K
+ }),
+ cancellationToken);
+ }
+
+ /// <summary>
+ /// Finds papers related to a paper.
+ /// </summary>
+ public async Task<SimilarPapersResponse> RelatedPapersAsync(
+ string paperId,
+ string intent,
+ RelatedPapersOptions? options = null,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(paperId);
+ ArgumentNullException.ThrowIfNull(intent);
+
+ return await _http.GetAsync<SimilarPapersResponse>(
+ $"/v2/search/research/papers/{Uri.EscapeDataString(paperId)}/similar"
+ + BuildResearchQuery(new Dictionary<string, object?>
+ {
+ ["intent"] = intent,
+ ["origin"] = SdkOrigin,
+ ["mode"] = options?.Mode,
+ ["k"] = options?.K,
+ ["rerank"] = options?.Rerank,
+ ["anchor"] = options?.Anchor
+ }),
+ cancellationToken);
+ }
+
+ /// <summary>
+ /// Searches GitHub research content.
+ /// </summary>
+ public async Task<GitHubSearchResponse> SearchGitHubAsync(
+ string query,
+ SearchGitHubOptions? options = null,
+ CancellationToken cancellationToken = default)
+ {
+ ArgumentNullException.ThrowIfNull(query);
+
+ return await _http.GetAsync<GitHubSearchResponse>(
+ "/v2/search/research/github" + BuildResearchQuery(new Dictionary<string, object?>
+ {
+ ["query"] = query,
+ ["origin"] = SdkOrigin,
+ ["k"] = options?.K
+ }),
+ cancellationToken);
+ }
+
// ================================================================
// USAGE & METRICS
// ================================================================
@@ -657,6 +764,39 @@ public class FirecrawlClient
return query.Count == 0 ? string.Empty : "?" + string.Join("&", query);
}
+ private static string BuildResearchQuery(Dictionary<string, object?> parameters)
+ {
+ var query = new List<string>();
+ foreach (var (key, value) in parameters)
+ {
+ if (value == null)
+ continue;
+
+ if (value is System.Collections.IEnumerable values && value is not string)
+ {
+ foreach (var item in values)
+ {
+ if (item != null)
+ AddQueryValue(query, key, item);
+ }
+ continue;
+ }
+
+ AddQueryValue(query, key, value);
+ }
+
+ return query.Count == 0 ? string.Empty : "?" + string.Join("&", query);
+ }
+
+ private static void AddQueryValue(List<string> query, string key, object value)
+ {
+ var stringValue = value is bool boolValue
+ ? boolValue.ToString().ToLowerInvariant()
+ : value.ToString() ?? string.Empty;
+
+ query.Add($"{Uri.EscapeDataString(key)}={Uri.EscapeDataString(stringValue)}");
+ }
+
private static string? ResolveApiKey(string? apiKey)
{
if (!string.IsNullOrWhiteSpace(apiKey))
diff --git a/apps/dot-net-sdk/Firecrawl/Models/ResearchModels.cs b/apps/dot-net-sdk/Firecrawl/Models/ResearchModels.cs
new file mode 100644
index 000000000..be794f5c2
--- /dev/null
+++ b/apps/dot-net-sdk/Firecrawl/Models/ResearchModels.cs
@@ -0,0 +1,141 @@
+using System.Text.Json.Serialization;
+
+namespace Firecrawl.Models;
+
+public class PaperResult
+{
+ [JsonPropertyName("paperId")]
+ public string? PaperId { get; set; }
+
+ [JsonPropertyName("primaryId")]
+ public string? PrimaryId { get; set; }
+
+ public Dictionary<string, object>? Ids { get; set; }
+ public string? Title { get; set; }
+ public string? Abstract { get; set; }
+ public double? Score { get; set; }
+ public int? Year { get; set; }
+ public List<string>? Authors { get; set; }
+ public string? Venue { get; set; }
+ public string? Url { get; set; }
+ public Dictionary<string, object>? Signals { get; set; }
+}
+
+public class PaperMetadata
+{
+ [JsonPropertyName("paperId")]
+ public string? PaperId { get; set; }
+
+ public Dictionary<string, object>? Ids { get; set; }
+ public string? Title { get; set; }
+ public string? Abstract { get; set; }
+ public string? Authors { get; set; }
+ public List<string>? Categories { get; set; }
+
+ [JsonPropertyName("createdDate")]
+ public string? CreatedDate { get; set; }
+
+ [JsonPropertyName("updateDate")]
+ public string? UpdateDate { get; set; }
+}
+
+public class Passage
+{
+ public string? Text { get; set; }
+ public string? Section { get; set; }
+ public int? Page { get; set; }
+ public double? Score { get; set; }
+ public Dictionary<string, object>? Metadata { get; set; }
+}
+
+public class SearchPapersResponse
+{
+ public bool Success { get; set; }
+ public List<PaperResult>? Results { get; set; }
+}
+
+public class PaperMetadataResponse
+{
+ public bool Success { get; set; }
+ public PaperMetadata? Paper { get; set; }
+}
+
+public class ReadPaperResponse
+{
+ public bool Success { get; set; }
+ public PaperMetadata? Paper { get; set; }
+
+ [JsonPropertyName("paperId")]
+ public string? PaperId { get; set; }
+
+ public string? Query { get; set; }
+ public List<Passage>? Passages { get; set; }
+}
+
+public class SimilarPapersResponse
+{
+ public bool Success { get; set; }
+ public List<PaperResult>? Results { get; set; }
+
+ [JsonPropertyName("poolSize")]
+ public int? PoolSize { get; set; }
+
+ public bool Truncated { get; set; }
+ public string? Note { get; set; }
+}
+
+public class GitHubSearchItem
+{
+ public string? ResultType { get; set; }
+
+ public string? Repo { get; set; }
+ public string? Url { get; set; }
+
+ public string? PageType { get; set; }
+
+ public int? Number { get; set; }
+
+ public int? SegmentCount { get; set; }
+
+ public string? ReadmeUrl { get; set; }
+
+ public string? Title { get; set; }
+ public string? Snippet { get; set; }
+
+ public string? ContentMd { get; set; }
+
+ public Dictionary<string, object>? Scores { get; set; }
+}
+
+public class GitHubSearchResponse
+{
+ public bool Success { get; set; }
+ public List<GitHubSearchItem>? Results { get; set; }
+}
+
+public class SearchPapersOptions
+{
+ public int? K { get; set; }
+ public List<string>? Authors { get; set; }
+ public List<string>? Categories { get; set; }
+ public string? From { get; set; }
+ public string? To { get; set; }
+}
+
+public class ReadPaperOptions
+{
+ public int? K { get; set; }
+}
+
+public class RelatedPapersOptions
+{
+ public string? Mode { get; set; }
+ public int? K { get; set; }
+ public bool? Rerank { get; set; }
+ public List<string>? Anchor { get; set; }
+}
+
+public class SearchGitHubOptions
+{
+ public int? K { get; set; }
+}
diff --git a/apps/elixir-sdk/lib/firecrawl.ex b/apps/elixir-sdk/lib/firecrawl.ex
index dc1ea6008..f81a95ecf 100644
--- a/apps/elixir-sdk/lib/firecrawl.ex
+++ b/apps/elixir-sdk/lib/firecrawl.ex
@@ -1196,6 +1196,149 @@ defmodule Firecrawl do
Req.post!(client(opts), url: "/search", json: to_body(params, @search_and_scrape_key_mapping))
end
+ @research_search_papers_schema NimbleOptions.new!([
+ authors: [type: {:list, :string}],
+ categories: [type: {:list, :string}],
+ from: [type: :string],
+ k: [type: :integer],
+ query: [type: :string, required: true],
+ to: [type: :string]
+ ])
+
+ @research_search_papers_key_mapping %{authors: "authors", categories: "categories", from: "from", k: "k", query: "query", to: "to"}
+
+ @doc """
+ Search research papers.
+
+ `GET /search/research/papers`
+ """
+ @spec search_papers(keyword(), keyword()) :: response()
+ def search_papers(params \\ [], opts \\ []) do
+ with {:ok, params} <- NimbleOptions.validate(params, @research_search_papers_schema) do
+ Req.get(client(opts), url: "/search/research/papers", params: [{"origin", @sdk_origin} | to_query(params, @research_search_papers_key_mapping)])
+ end
+ end
+
+ @doc """
+ Bang variant of `search_papers`. Raises on error.
+ """
+ @spec search_papers!(keyword(), keyword()) :: Req.Response.t()
+ def search_papers!(params \\ [], opts \\ []) do
+ params = NimbleOptions.validate!(params, @research_search_papers_schema)
+ Req.get!(client(opts), url: "/search/research/papers", params: [{"origin", @sdk_origin} | to_query(params, @research_search_papers_key_mapping)])
+ end
+
+ @doc """
+ Inspect paper metadata.
+
+ `GET /search/research/papers/{id}`
+ """
+ @spec inspect_paper(String.t(), keyword()) :: response()
+ def inspect_paper(id, opts \\ []) do
+ Req.get(client(opts),
+ url: "/search/research/papers/#{URI.encode_www_form(id)}",
+ params: [{"origin", @sdk_origin}]
+ )
+ end
+
+ @doc """
+ Bang variant of `inspect_paper`. Raises on error.
+ """
+ @spec inspect_paper!(String.t(), keyword()) :: Req.Response.t()
+ def inspect_paper!(id, opts \\ []) do
+ Req.get!(client(opts),
+ url: "/search/research/papers/#{URI.encode_www_form(id)}",
+ params: [{"origin", @sdk_origin}]
+ )
+ end
+
+ @research_read_paper_schema NimbleOptions.new!([
+ k: [type: :integer],
+ query: [type: :string, required: true]
+ ])
+
+ @research_read_paper_key_mapping %{k: "k", query: "query"}
+
+ @doc """
+ Read a paper with query-guided passages.
+
+ `GET /search/research/papers/{id}`
+ """
+ @spec read_paper(String.t(), keyword(), keyword()) :: response()
+ def read_paper(id, params \\ [], opts \\ []) do
+ with {:ok, params} <- NimbleOptions.validate(params, @research_read_paper_schema) do
+ Req.get(client(opts), url: "/search/research/papers/#{URI.encode_www_form(id)}", params: [{"origin", @sdk_origin} | to_query(params, @research_read_paper_key_mapping)])
+ end
+ end
+
+ @doc """
+ Bang variant of `read_paper`. Raises on error.
+ """
+ @spec read_paper!(String.t(), keyword(), keyword()) :: Req.Response.t()
+ def read_paper!(id, params \\ [], opts \\ []) do
+ params = NimbleOptions.validate!(params, @research_read_paper_schema)
+ Req.get!(client(opts), url: "/search/research/papers/#{URI.encode_www_form(id)}", params: [{"origin", @sdk_origin} | to_query(params, @research_read_paper_key_mapping)])
+ end
+
+ @research_related_papers_schema NimbleOptions.new!([
+ anchor: [type: {:list, :string}],
+ intent: [type: :string, required: true],
+ k: [type: :integer],
+ mode: [type: {:in, [:similar, :citers, :references, "similar", "citers", "references"]}],
+ rerank: [type: :boolean]
+ ])
+
+ @research_related_papers_key_mapping %{anchor: "anchor", intent: "intent", k: "k", mode: "mode", rerank: "rerank"}
+
+ @doc """
+ Find papers related to a paper.
+
+ `GET /search/research/papers/{id}/similar`
+ """
+ @spec related_papers(String.t(), keyword(), keyword()) :: response()
+ def related_papers(id, params \\ [], opts \\ []) do
+ with {:ok, params} <- NimbleOptions.validate(params, @research_related_papers_schema) do
+ Req.get(client(opts), url: "/search/research/papers/#{URI.encode_www_form(id)}/similar", params: [{"origin", @sdk_origin} | to_query(params, @research_related_papers_key_mapping)])
+ end
+ end
+
+ @doc """
+ Bang variant of `related_papers`. Raises on error.
+ """
+ @spec related_papers!(String.t(), keyword(), keyword()) :: Req.Response.t()
+ def related_papers!(id, params \\ [], opts \\ []) do
+ params = NimbleOptions.validate!(params, @research_related_papers_schema)
+ Req.get!(client(opts), url: "/search/research/papers/#{URI.encode_www_form(id)}/similar", params: [{"origin", @sdk_origin} | to_query(params, @research_related_papers_key_mapping)])
+ end
+
+ @research_search_github_schema NimbleOptions.new!([
+ k: [type: :integer],
+ query: [type: :string, required: true]
+ ])
+
+ @research_search_github_key_mapping %{k: "k", query: "query"}
+
+ @doc """
+ Search GitHub research content.
+
+ `GET /search/research/github`
+ """
+ @spec search_github(keyword(), keyword()) :: response()
+ def search_github(params \\ [], opts \\ []) do
+ with {:ok, params} <- NimbleOptions.validate(params, @research_search_github_schema) do
+ Req.get(client(opts), url: "/search/research/github", params: [{"origin", @sdk_origin} | to_query(params, @research_search_github_key_mapping)])
+ end
+ end
+
+ @doc """
+ Bang variant of `search_github`. Raises on error.
+ """
+ @spec search_github!(keyword(), keyword()) :: Req.Response.t()
+ def search_github!(params \\ [], opts \\ []) do
+ params = NimbleOptions.validate!(params, @research_search_github_schema)
+ Req.get!(client(opts), url: "/search/research/github", params: [{"origin", @sdk_origin} | to_query(params, @research_search_github_key_mapping)])
+ end
+
@start_agent_schema NimbleOptions.new!([
max_credits: [type: {:or, [:integer, :float]}, doc: "Maximum credits to spend on this agent task. Defaults to 2500 if not set. Values above 2,500 are always billed as paid requests."],
diff --git a/apps/go-sdk/firecrawl.go b/apps/go-sdk/firecrawl.go
index 84495c3e0..409bb2e9a 100644
--- a/apps/go-sdk/firecrawl.go
+++ b/apps/go-sdk/firecrawl.go
@@ -557,6 +557,142 @@ func (c *Client) Search(ctx context.Context, query string, opts *SearchOptions)
return data, nil
}
+// SearchPapers searches research papers.
+func (c *Client) SearchPapers(ctx context.Context, query string, opts *SearchPapersOptions) (*SearchPapersResponse, error) {
+ if query == "" {
+ return nil, &FirecrawlError{Message: "query is required"}
+ }
+ values := url.Values{}
+ values.Set("query", query)
+ values.Set("origin", "go-sdk@"+Version)
+ if opts != nil {
+ if opts.K != nil {
+ values.Set("k", fmt.Sprint(*opts.K))
+ }
+ for _, author := range opts.Authors {
+ values.Add("authors", author)
+ }
+ for _, category := range opts.Categories {
+ values.Add("categories", category)
+ }
+ if opts.From != "" {
+ values.Set("from", opts.From)
+ }
+ if opts.To != "" {
+ values.Set("to", opts.To)
+ }
+ }
+
+ raw, err := c.http.get(ctx, "/v2/search/research/papers?"+values.Encode())
+ if err != nil {
+ return nil, err
+ }
+ var resp SearchPapersResponse
+ if err := json.Unmarshal(raw, &resp); err != nil {
+ return nil, &FirecrawlError{Message: fmt.Sprintf("failed to decode response: %v", err)}
+ }
+ return &resp, nil
+}
+
+// InspectPaper fetches paper metadata.
+func (c *Client) InspectPaper(ctx context.Context, paperID string) (*PaperMetadataResponse, error) {
+ if paperID == "" {
+ return nil, &FirecrawlError{Message: "paper ID is required"}
+ }
+ raw, err := c.http.get(ctx, "/v2/search/research/papers/"+url.PathEscape(paperID))
+ if err != nil {
+ return nil, err
+ }
+ var resp PaperMetadataResponse
+ if err := json.Unmarshal(raw, &resp); err != nil {
+ return nil, &FirecrawlError{Message: fmt.Sprintf("failed to decode response: %v", err)}
+ }
+ return &resp, nil
+}
+
+// ReadPaper fetches paper metadata and relevant passages.
+func (c *Client) ReadPaper(ctx context.Context, paperID, query string, opts *ReadPaperOptions) (*ReadPaperResponse, error) {
+ if paperID == "" {
+ return nil, &FirecrawlError{Message: "paper ID is required"}
+ }
+ if query == "" {
+ return nil, &FirecrawlError{Message: "query is required"}
+ }
+ values := url.Values{}
+ values.Set("query", query)
+ values.Set("origin", "go-sdk@"+Version)
+ if opts != nil && opts.K != nil {
+ values.Set("k", fmt.Sprint(*opts.K))
+ }
+ raw, err := c.http.get(ctx, "/v2/search/research/papers/"+url.PathEscape(paperID)+"?"+values.Encode())
+ if err != nil {
+ return nil, err
+ }
+ var resp ReadPaperResponse
+ if err := json.Unmarshal(raw, &resp); err != nil {
+ return nil, &FirecrawlError{Message: fmt.Sprintf("failed to decode response: %v", err)}
+ }
+ return &resp, nil
+}
+
+// RelatedPapers finds papers related to a paper.
+func (c *Client) RelatedPapers(ctx context.Context, paperID, intent string, opts *RelatedPapersOptions) (*SimilarPapersResponse, error) {
+ if paperID == "" {
+ return nil, &FirecrawlError{Message: "paper ID is required"}
+ }
+ if intent == "" {
+ return nil, &FirecrawlError{Message: "intent is required"}
+ }
+ values := url.Values{}
+ values.Set("intent", intent)
+ values.Set("origin", "go-sdk@"+Version)
+ if opts != nil {
+ if opts.Mode != "" {
+ values.Set("mode", opts.Mode)
+ }
+ if opts.K != nil {
+ values.Set("k", fmt.Sprint(*opts.K))
+ }
+ if opts.Rerank != nil {
+ values.Set("rerank", fmt.Sprint(*opts.Rerank))
+ }
+ for _, anchor := range opts.Anchor {
+ values.Add("anchor", anchor)
+ }
+ }
+ raw, err := c.http.get(ctx, "/v2/search/research/papers/"+url.PathEscape(paperID)+"/similar?"+values.Encode())
+ if err != nil {
+ return nil, err
+ }
+ var resp SimilarPapersResponse
+ if err := json.Unmarshal(raw, &resp); err != nil {
+ return nil, &FirecrawlError{Message: fmt.Sprintf("failed to decode response: %v", err)}
+ }
+ return &resp, nil
+}
+
+// SearchGitHub searches GitHub research content.
+func (c *Client) SearchGitHub(ctx context.Context, query string, opts *SearchGitHubOptions) (*GitHubSearchResponse, error) {
+ if query == "" {
+ return nil, &FirecrawlError{Message: "query is required"}
+ }
+ values := url.Values{}
+ values.Set("query", query)
+ values.Set("origin", "go-sdk@"+Version)
+ if opts != nil && opts.K != nil {
+ values.Set("k", fmt.Sprint(*opts.K))
+ }
+ raw, err := c.http.get(ctx, "/v2/search/research/github?"+values.Encode())
+ if err != nil {
+ return nil, err
+ }
+ var resp GitHubSearchResponse
+ if err := json.Unmarshal(raw, &resp); err != nil {
+ return nil, &FirecrawlError{Message: fmt.Sprintf("failed to decode response: %v", err)}
+ }
+ return &resp, nil
+}
+
// ================================================================
// AGENT
// ================================================================
diff --git a/apps/go-sdk/models.go b/apps/go-sdk/models.go
index 858bfcce9..51d3a1541 100644
--- a/apps/go-sdk/models.go
+++ b/apps/go-sdk/models.go
@@ -24,6 +24,120 @@ type Document struct {
Branding map[string]interface{} `json:"branding,omitempty"`
}
+// PaperResult represents a ranked research paper result.
+type PaperResult struct {
+ PaperID string `json:"paperId"`
+ PrimaryID string `json:"primaryId,omitempty"`
+ IDs map[string]interface{} `json:"ids,omitempty"`
+ Title string `json:"title,omitempty"`
+ Abstract string `json:"abstract,omitempty"`
+ Score *float64 `json:"score,omitempty"`
+ Year *int `json:"year,omitempty"`
+ Authors []string `json:"authors,omitempty"`
+ Venue string `json:"venue,omitempty"`
+ URL string `json:"url,omitempty"`
+ Signals map[string]interface{} `json:"signals,omitempty"`
+}
+
+// PaperMetadata represents paper metadata returned by inspect/read endpoints.
+type PaperMetadata struct {
+ PaperID string `json:"paperId,omitempty"`
+ IDs map[string]interface{} `json:"ids,omitempty"`
+ Title string `json:"title,omitempty"`
+ Abstract string `json:"abstract,omitempty"`
+ Authors string `json:"authors,omitempty"`
+ Categories []string `json:"categories,omitempty"`
+ CreatedDate string `json:"createdDate,omitempty"`
+ UpdateDate string `json:"updateDate,omitempty"`
+}
+
+// Passage is a relevant paper passage.
+type Passage struct {
+ Text string `json:"text,omitempty"`
+ Section string `json:"section,omitempty"`
+ Page *int `json:"page,omitempty"`
+ Score *float64 `json:"score,omitempty"`
+ Metadata map[string]interface{} `json:"metadata,omitempty"`
+}
+
+// SearchPapersResponse is returned by SearchPapers.
+type SearchPapersResponse struct {
+ Success bool `json:"success"`
+ Results []PaperResult `json:"results"`
+}
+
+// PaperMetadataResponse is returned by InspectPaper.
+type PaperMetadataResponse struct {
+ Success bool `json:"success"`
+ Paper PaperMetadata `json:"paper"`
+}
+
+// ReadPaperResponse is returned by ReadPaper.
+type ReadPaperResponse struct {
+ Success bool `json:"success"`
+ Paper PaperMetadata `json:"paper"`
+ PaperID string `json:"paperId,omitempty"`
+ Query string `json:"query,omitempty"`
+ Passages []Passage `json:"passages,omitempty"`
+}
+
+// SimilarPapersResponse is returned by RelatedPapers.
+type SimilarPapersResponse struct {
+ Success bool `json:"success"`
+ Results []PaperResult `json:"results"`
+ PoolSize *int `json:"poolSize,omitempty"`
+ Truncated bool `json:"truncated"`
+ Note *string `json:"note,omitempty"`
+}
+
+// GitHubSearchItem represents a GitHub research search result.
+type GitHubSearchItem struct {
+ ResultType string `json:"resultType,omitempty"`
+ Repo string `json:"repo,omitempty"`
+ URL string `json:"url,omitempty"`
+ PageType string `json:"pageType,omitempty"`
+ Number *int `json:"number,omitempty"`
+ SegmentCount *int `json:"segmentCount,omitempty"`
+ ReadmeURL string `json:"readmeUrl,omitempty"`
+ Title string `json:"title,omitempty"`
+ Snippet string `json:"snippet,omitempty"`
+ ContentMD string `json:"contentMd,omitempty"`
+ Scores map[string]interface{} `json:"scores,omitempty"`
+}
+
+// GitHubSearchResponse is returned by SearchGitHub.
+type GitHubSearchResponse struct {
+ Success bool `json:"success"`
+ Results []GitHubSearchItem `json:"results"`
+}
+
+// Research options for search-like endpoints.
+type SearchPapersOptions struct {
+ K *int `json:"k,omitempty"`
+ Authors []string `json:"authors,omitempty"`
+ Categories []string `json:"categories,omitempty"`
+ From string `json:"from,omitempty"`
+ To string `json:"to,omitempty"`
+}
+
+// ReadPaperOptions configures ReadPaper.
+type ReadPaperOptions struct {
+ K *int `json:"k,omitempty"`
+}
+
+// RelatedPapersOptions configures RelatedPapers.
+type RelatedPapersOptions struct {
+ Mode string `json:"mode,omitempty"`
+ K *int `json:"k,omitempty"`
+ Rerank *bool `json:"rerank,omitempty"`
+ Anchor []string `json:"anchor,omitempty"`
+}
+
+// SearchGitHubOptions configures SearchGitHub.
+type SearchGitHubOptions struct {
+ K *int `json:"k,omitempty"`
+}
+
// CrawlResponse is returned when starting an async crawl.
type CrawlResponse struct {
ID string `json:"id"`
diff --git a/apps/java-sdk/src/main/java/com/firecrawl/client/FirecrawlClient.java b/apps/java-sdk/src/main/java/com/firecrawl/client/FirecrawlClient.java
index 71688c500..a2b43e53b 100644
--- a/apps/java-sdk/src/main/java/com/firecrawl/client/FirecrawlClient.java
+++ b/apps/java-sdk/src/main/java/com/firecrawl/client/FirecrawlClient.java
@@ -556,6 +556,65 @@ public class FirecrawlClient {
return extractData(http.post("/v2/search", body, Map.class), SearchData.class);
}
+ public ResearchModels.SearchPapersResponse searchPapers(String query) {
+ return searchPapers(query, null);
+ }
+
+ public ResearchModels.SearchPapersResponse searchPapers(String query, ResearchModels.SearchPapersOptions options) {
+ Objects.requireNonNull(query, "Query is required");
+ Map<String, Object> params = new LinkedHashMap<>();
+ params.put("query", query);
+ params.put("origin", SDK_ORIGIN);
+ if (options != null) mergeOptions(params, options);
+ return http.get("/v2/search/research/papers" + researchQuery(params), ResearchModels.SearchPapersResponse.class);
+ }
+
+ public ResearchModels.PaperMetadataResponse inspectPaper(String paperId) {
+ Objects.requireNonNull(paperId, "Paper ID is required");
+ return http.get("/v2/search/research/papers/" + urlEncode(paperId), ResearchModels.PaperMetadataResponse.class);
+ }
+
+ public ResearchModels.ReadPaperResponse readPaper(String paperId, String query) {
+ return readPaper(paperId, query, null);
+ }
+
+ public ResearchModels.ReadPaperResponse readPaper(String paperId, String query, ResearchModels.ReadPaperOptions options) {
+ Objects.requireNonNull(paperId, "Paper ID is required");
+ Objects.requireNonNull(query, "Query is required");
+ Map<String, Object> params = new LinkedHashMap<>();
+ params.put("query", query);
+ params.put("origin", SDK_ORIGIN);
+ if (options != null) mergeOptions(params, options);
+ return http.get("/v2/search/research/papers/" + urlEncode(paperId) + researchQuery(params), ResearchModels.ReadPaperResponse.class);
+ }
+
+ public ResearchModels.SimilarPapersResponse relatedPapers(String paperId, String intent) {
+ return relatedPapers(paperId, intent, null);
+ }
+
+ public ResearchModels.SimilarPapersResponse relatedPapers(String paperId, String intent, ResearchModels.RelatedPapersOptions options) {
+ Objects.requireNonNull(paperId, "Paper ID is required");
+ Objects.requireNonNull(intent, "Intent is required");
+ Map<String, Object> params = new LinkedHashMap<>();
+ params.put("intent", intent);
+ params.put("origin", SDK_ORIGIN);
+ if (options != null) mergeOptions(params, options);
+ return http.get("/v2/search/research/papers/" + urlEncode(paperId) + "/similar" + researchQuery(params), ResearchModels.SimilarPapersResponse.class);
+ }
+
+ public ResearchModels.GitHubSearchResponse searchGitHub(String query) {
+ return searchGitHub(query, null);
+ }
+
+ public ResearchModels.GitHubSearchResponse searchGitHub(String query, ResearchModels.SearchGitHubOptions options) {
+ Objects.requireNonNull(query, "Query is required");
+ Map<String, Object> params = new LinkedHashMap<>();
+ params.put("query", query);
+ params.put("origin", SDK_ORIGIN);
+ if (options != null) mergeOptions(params, options);
+ return http.get("/v2/search/research/github" + researchQuery(params), ResearchModels.GitHubSearchResponse.class);
+ }
+
// ================================================================
// AGENT
// ================================================================
@@ -913,6 +972,26 @@ public class FirecrawlClient {
return CompletableFuture.supplyAsync(() -> search(query, options), asyncExecutor);
}
+ public CompletableFuture<ResearchModels.SearchPapersResponse> searchPapersAsync(String query, ResearchModels.SearchPapersOptions options) {
+ return CompletableFuture.supplyAsync(() -> searchPapers(query, options), asyncExecutor);
+ }
+
+ public CompletableFuture<ResearchModels.PaperMetadataResponse> inspectPaperAsync(String paperId) {
+ return CompletableFuture.supplyAsync(() -> inspectPaper(paperId), asyncExecutor);
+ }
+
+ public CompletableFuture<ResearchModels.ReadPaperResponse> readPaperAsync(String paperId, String query, ResearchModels.ReadPaperOptions options) {
+ return CompletableFuture.supplyAsync(() -> readPaper(paperId, query, options), asyncExecutor);
+ }
+
+ public CompletableFuture<ResearchModels.SimilarPapersResponse> relatedPapersAsync(String paperId, String intent, ResearchModels.RelatedPapersOptions options) {
+ return CompletableFuture.supplyAsync(() -> relatedPapers(paperId, intent, options), asyncExecutor);
+ }
+
+ public CompletableFuture<ResearchModels.GitHubSearchResponse> searchGitHubAsync(String query, ResearchModels.SearchGitHubOptions options) {
+ return CompletableFuture.supplyAsync(() -> searchGitHub(query, options), asyncExecutor);
+ }
+
/**
* Asynchronously runs a map operation.
*
@@ -1264,6 +1343,30 @@ public class FirecrawlClient {
return parts.isEmpty() ? "" : "?" + String.join("&", parts);
}
+ private String researchQuery(Map<String, Object> params) {
+ List<String> parts = new ArrayList<>();
+ for (Map.Entry<String, Object> entry : params.entrySet()) {
+ Object value = entry.getValue();
+ if (value == null) continue;
+ if (value instanceof Collection<?>) {
+ for (Object item : (Collection<?>) value) {
+ if (item != null) parts.add(urlEncode(entry.getKey()) + "=" + urlEncode(stringValue(item)));
+ }
+ } else {
+ parts.add(urlEncode(entry.getKey()) + "=" + urlEncode(stringValue(value)));
+ }
+ }
+ return parts.isEmpty() ? "" : "?" + String.join("&", parts);
+ }
+
+ private String stringValue(Object value) {
+ return value instanceof Boolean ? value.toString().toLowerCase(Locale.ROOT) : value.toString();
+ }
+
+ private String urlEncode(String value) {
+ return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8);
+ }
+
/**
* Merges a typed options object into a request body map, using Jackson serialization.
*/
diff --git a/apps/java-sdk/src/main/java/com/firecrawl/models/ResearchModels.java b/apps/java-sdk/src/main/java/com/firecrawl/models/ResearchModels.java
new file mode 100644
index 000000000..6243119c1
--- /dev/null
+++ b/apps/java-sdk/src/main/java/com/firecrawl/models/ResearchModels.java
@@ -0,0 +1,119 @@
+package com.firecrawl.models;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.List;
+import java.util.Map;
+
+public final class ResearchModels {
+ private ResearchModels() {}
+
+ public static class PaperResult {
+ @JsonProperty("paperId")
+ public String paperId;
+ @JsonProperty("primaryId")
+ public String primaryId;
+ public Map<String, Object> ids;
+ public String title;
+ @JsonProperty("abstract")
+ public String abstractText;
+ public Double score;
+ public Integer year;
+ public List<String> authors;
+ public String venue;
+ public String url;
+ public Map<String, Object> signals;
+ }
+
+ public static class PaperMetadata {
+ @JsonProperty("paperId")
+ public String paperId;
+ public Map<String, Object> ids;
+ public String title;
+ @JsonProperty("abstract")
+ public String abstractText;
+ public String authors;
+ public List<String> categories;
+ @JsonProperty("createdDate")
+ public String createdDate;
+ @JsonProperty("updateDate")
+ public String updateDate;
+ }
+
+ public static class Passage {
+ public String text;
+ public String section;
+ public Integer page;
+ public Double score;
+ public Map<String, Object> metadata;
+ }
+
+ public static class SearchPapersResponse {
+ public boolean success;
+ public List<PaperResult> results;
+ }
+
+ public static class PaperMetadataResponse {
+ public boolean success;
+ public PaperMetadata paper;
+ }
+
+ public static class ReadPaperResponse {
+ public boolean success;
+ public PaperMetadata paper;
+ @JsonProperty("paperId")
+ public String paperId;
+ public String query;
+ public List<Passage> passages;
+ }
+
+ public static class SimilarPapersResponse {
+ public boolean success;
+ public List<PaperResult> results;
+ @JsonProperty("poolSize")
+ public Integer poolSize;
+ public boolean truncated;
+ public String note;
+ }
+
+ public static class GitHubSearchItem {
+ public String resultType;
+ public String repo;
+ public String url;
+ public String pageType;
+ public Integer number;
+ public Integer segmentCount;
+ public String readmeUrl;
+ public String title;
+ public String snippet;
+ public String contentMd;
+ public Map<String, Object> scores;
+ }
+
+ public static class GitHubSearchResponse {
+ public boolean success;
+ public List<GitHubSearchItem> results;
+ }
+
+ public static class SearchPapersOptions {
+ public Integer k;
+ public List<String> authors;
+ public List<String> categories;
+ public String from;
+ public String to;
+ }
+
+ public static class ReadPaperOptions {
+ public Integer k;
+ }
+
+ public static class RelatedPapersOptions {
+ public String mode;
+ public Integer k;
+ public Boolean rerank;
+ public List<String> anchor;
+ }
+
+ public static class SearchGitHubOptions {
+ public Integer k;
+ }
+}
diff --git a/apps/js-sdk/firecrawl/src/__tests__/unit/v2/research.test.ts b/apps/js-sdk/firecrawl/src/__tests__/unit/v2/research.test.ts
index 93d76558f..9e1995d38 100644
--- a/apps/js-sdk/firecrawl/src/__tests__/unit/v2/research.test.ts
+++ b/apps/js-sdk/firecrawl/src/__tests__/unit/v2/research.test.ts
@@ -39,7 +39,7 @@ describe("research.searchPapers", () => {
to: "2024-12-31",
});
const url = calls[0];
- expect(url.startsWith("/v2/research/papers?")).toBe(true);
+ expect(url.startsWith("/v2/search/research/papers?")).toBe(true);
const qs = new URLSearchParams(url.split("?")[1]);
expect(qs.get("query")).toBe("diffusion models");
expect(qs.get("k")).toBe("10");
@@ -67,7 +67,7 @@ describe("research.searchPapers", () => {
});
test("returns the response body verbatim", async () => {
- const payload = { results: [{ paper_id: "1", title: "t", abstract: "a", score: 0.1 }] };
+ const payload = { results: [{ paperId: "1", title: "t", abstract: "a", score: 0.1 }] };
const { client } = makeClient(() => ({ status: 200, data: payload }));
await expect(client.searchPapers("q")).resolves.toEqual(payload);
});
@@ -77,17 +77,17 @@ describe("research.getPaper", () => {
test("detail mode encodes the id and sends no query params", async () => {
const { client, calls } = makeClient(() => ({ status: 200, data: { paper: {} } }));
await client.getPaper("arxiv:2105.05233");
- expect(calls[0]).toBe("/v2/research/papers/arxiv%3A2105.05233");
+ expect(calls[0]).toBe("/v2/search/research/papers/arxiv%3A2105.05233");
});
test("read mode adds query and k", async () => {
const { client, calls } = makeClient(() => ({
status: 200,
- data: { paper: {}, paper_id: "1", query: "q", passages: [] },
+ data: { paper: {}, paperId: "1", query: "q", passages: [] },
}));
await client.getPaper("123", { query: "noise schedule", k: 4 });
const [path, query] = calls[0].split("?");
- expect(path).toBe("/v2/research/papers/123");
+ expect(path).toBe("/v2/search/research/papers/123");
const qs = new URLSearchParams(query);
expect(qs.get("query")).toBe("noise schedule");
expect(qs.get("k")).toBe("4");
@@ -112,7 +112,7 @@ describe("research.similarPapers", () => {
test("builds path and query with repeated anchors and rerank", async () => {
const { client, calls } = makeClient(() => ({
status: 200,
- data: { results: [], pool_size: 0, truncated: false },
+ data: { results: [], poolSize: 0, truncated: false },
}));
await client.similarPapers("2105.05233", {
intent: "diffusion image synthesis",
@@ -122,7 +122,7 @@ describe("research.similarPapers", () => {
anchor: ["arxiv:2006.11239", "1503.03585"],
});
const [path, query] = calls[0].split("?");
- expect(path).toBe("/v2/research/papers/2105.05233/similar");
+ expect(path).toBe("/v2/search/research/papers/2105.05233/similar");
const qs = new URLSearchParams(query);
expect(qs.get("intent")).toBe("diffusion image synthesis");
expect(qs.get("mode")).toBe("citers");
@@ -137,7 +137,7 @@ describe("research.searchGithub", () => {
const { client, calls } = makeClient(() => ({ status: 200, data: { results: [] } }));
await client.searchGithub("milvus hybrid search", { k: 10 });
const qs = new URLSearchParams(calls[0].split("?")[1]);
- expect(calls[0].startsWith("/v2/research/github?")).toBe(true);
+ expect(calls[0].startsWith("/v2/search/research/github?")).toBe(true);
expect(qs.get("query")).toBe("milvus hybrid search");
expect(qs.get("k")).toBe("10");
});
diff --git a/apps/js-sdk/firecrawl/src/v2/methods/research.ts b/apps/js-sdk/firecrawl/src/v2/methods/research.ts
index c84bf70ce..c764c4400 100644
--- a/apps/js-sdk/firecrawl/src/v2/methods/research.ts
+++ b/apps/js-sdk/firecrawl/src/v2/methods/research.ts
@@ -13,7 +13,7 @@ import { SdkError } from "../types";
import { HttpClient } from "../utils/httpClient";
import { throwForBadResponse } from "../utils/errorHandler";
-const BASE = "/v2/research";
+const BASE = "/v2/search/research";
/** Append a value (or repeated array values) to a URLSearchParams instance. */
function appendParam(
diff --git a/apps/js-sdk/firecrawl/src/v2/types.ts b/apps/js-sdk/firecrawl/src/v2/types.ts
index edc7a8378..d617335dd 100644
--- a/apps/js-sdk/firecrawl/src/v2/types.ts
+++ b/apps/js-sdk/firecrawl/src/v2/types.ts
@@ -1175,16 +1175,18 @@ export interface PaperSignals {
structural: number;
/** Semantic score from the intent abstract search (0 if absent). */
semantic: number;
- /** Citation-graph PageRank of the candidate. */
- pagerank: number;
+ /** Citation-graph article-rank score of the candidate. */
+ articleRank: number;
/** Number of distinct seeds connected to this candidate. */
- seed_overlap: number;
+ seedOverlap: number;
}
-/** A ranked paper. `paper_id` is canonical; arXiv lives in `ids`. */
+/** A ranked paper. `paperId` is canonical; arXiv lives in `ids`. */
export interface PaperResult {
/** Canonical paper id — the Milvus INT64 primary key as a decimal string. */
- paper_id: string;
+ paperId: string;
+ /** Preferred cite/fetch identifier such as `arxiv:<id>`, `pmid:<id>`, or `doi:<id>`. */
+ primaryId: string;
ids?: IdMap;
title: string;
abstract: string;
@@ -1195,7 +1197,7 @@ export interface PaperResult {
}
export interface PaperMetadata {
- paper_id: string;
+ paperId: string;
ids?: IdMap;
title: string;
abstract: string;
@@ -1204,9 +1206,9 @@ export interface PaperMetadata {
/** arXiv categories. Omitted if unknown. */
categories?: string[];
/** Original creation date string (format varies). Omitted if unknown. */
- created_date?: string;
+ createdDate?: string;
/** Last-updated date string. Omitted if unknown. */
- update_date?: string;
+ updateDate?: string;
}
export interface Passage {
@@ -1217,17 +1219,20 @@ export interface Passage {
}
export interface SearchPapersResponse {
+ success: boolean;
results: PaperResult[];
}
export interface PaperMetadataResponse {
+ success: boolean;
paper: PaperMetadata;
}
export interface ReadPaperResponse {
+ success: boolean;
paper: PaperMetadata;
/** Resolved canonical paper id (empty string if not found via id-key). */
- paper_id: string;
+ paperId: string;
/** Echo of the read query. */
query: string;
/** Top matching in-body passages. */
@@ -1235,10 +1240,11 @@ export interface ReadPaperResponse {
}
export interface SimilarPapersResponse {
+ success: boolean;
/** Ranked related papers; each carries `signals`. */
results: PaperResult[];
/** Number of resolved candidates considered before truncation to `k`. */
- pool_size: number;
+ poolSize: number;
/** True if more resolved candidates existed than were returned. */
truncated: boolean;
/** Human-readable note when no results are produced. */
@@ -1251,11 +1257,12 @@ export interface GitHubScoreBreakdown {
semantic?: number;
lexical?: number;
fusion?: number;
+ rerank?: number;
}
export interface GitHubSearchItem {
- resultType: "github_history" | "repo_readme";
- /** `owner/name`. */
+ resultType: "github_history" | "repo_readme" | "web";
+ /** `owner/name`; empty for web results whose URL is not a repo page. */
repo: string;
url: string;
/** History page type (e.g. `issue`, `pull`). Omitted for readmes. */
@@ -1266,6 +1273,8 @@ export interface GitHubSearchItem {
segmentCount?: number;
/** Readme URL (readme results). Omitted otherwise. */
readmeUrl?: string;
+ /** SERP page title. Only set on web results. */
+ title?: string;
/** Short matched excerpt. */
snippet: string;
/** Full matched content in markdown. Omitted unless available. */
@@ -1274,6 +1283,7 @@ export interface GitHubSearchItem {
}
export interface GitHubSearchResponse {
+ success: boolean;
results: GitHubSearchItem[];
}
diff --git a/apps/php-sdk/src/Client/FirecrawlClient.php b/apps/php-sdk/src/Client/FirecrawlClient.php
index 9758ca9fe..8660c0c91 100644
--- a/apps/php-sdk/src/Client/FirecrawlClient.php
+++ b/apps/php-sdk/src/Client/FirecrawlClient.php
@@ -117,6 +117,78 @@ final class FirecrawlClient
return Document::fromArray($response['data'] ?? $response);
}
+ /**
+ * Search research papers.
+ *
+ * @param array<string, mixed> $options
+ * @return array<string, mixed>
+ */
+ public function searchPapers(string $query, array $options = []): array
+ {
+ return $this->http->get('/v2/search/research/papers' . $this->queryArray(array_merge(
+ ['query' => $query, 'origin' => 'php-sdk@' . Version::SDK_VERSION],
+ $options,
+ )));
+ }
+
+ /**
+ * Inspect paper metadata.
+ *
+ * @return array<string, mixed>
+ */
+ public function inspectPaper(string $paperId): array
+ {
+ return $this->http->get('/v2/search/research/papers/' . rawurlencode($paperId));
+ }
+
+ /**
+ * Read a paper with query-guided passages.
+ *
+ * @param array<string, mixed> $options
+ * @return array<string, mixed>
+ */
+ public function readPaper(string $paperId, string $query, array $options = []): array
+ {
+ return $this->http->get(
+ '/v2/search/research/papers/' . rawurlencode($paperId)
+ . $this->queryArray(array_merge(
+ ['query' => $query, 'origin' => 'php-sdk@' . Version::SDK_VERSION],
+ $options,
+ )),
+ );
+ }
+
+ /**
+ * Find papers related to a paper.
+ *
+ * @param array<string, mixed> $options
+ * @return array<string, mixed>
+ */
+ public function relatedPapers(string $paperId, string $intent, array $options = []): array
+ {
+ return $this->http->get(
+ '/v2/search/research/papers/' . rawurlencode($paperId) . '/similar'
+ . $this->queryArray(array_merge(
+ ['intent' => $intent, 'origin' => 'php-sdk@' . Version::SDK_VERSION],
+ $options,
+ )),
+ );
+ }
+
+ /**
+ * Search GitHub research content.
+ *
+ * @param array<string, mixed> $options
+ * @return array<string, mixed>
+ */
+ public function searchGithub(string $query, array $options = []): array
+ {
+ return $this->http->get('/v2/search/research/github' . $this->queryArray(array_merge(
+ ['query' => $query, 'origin' => 'php-sdk@' . Version::SDK_VERSION],
+ $options,
+ )));
+ }
+
/**
* Interact with the scrape-bound browser session for a scrape job.
*/
@@ -660,6 +732,29 @@ final class FirecrawlClient
return $params === [] ? '' : '?' . http_build_query($params);
}
+ /**
+ * @param array<string, mixed> $params
+ */
+ private function queryArray(array $params): string
+ {
+ $pairs = [];
+ foreach ($params as $key => $value) {
+ if ($value === null || $value === '') {
+ continue;
+ }
+ $values = is_array($value) ? $value : [$value];
+ foreach ($values as $item) {
+ if ($item === null || $item === '') {
+ continue;
+ }
+ $stringValue = is_bool($item) ? ($item ? 'true' : 'false') : (string) $item;
+ $pairs[] = rawurlencode((string) $key) . '=' . rawurlencode($stringValue);
+ }
+ }
+
+ return $pairs === [] ? '' : '?' . implode('&', $pairs);
+ }
+
private function pollCrawl(
?string $jobId,
int $pollIntervalSec,
diff --git a/apps/php-sdk/src/Client/FirecrawlHttpClient.php b/apps/php-sdk/src/Client/FirecrawlHttpClient.php
index 44e7c8d20..0b2284d7d 100644
--- a/apps/php-sdk/src/Client/FirecrawlHttpClient.php
+++ b/apps/php-sdk/src/Client/FirecrawlHttpClient.php
@@ -141,7 +141,7 @@ final class FirecrawlHttpClient
];
// Omit the Authorization header entirely when no key is set so that
// scrape/search/interact can use the keyless free tier.
- if ($this->apiKey !== null && $this->apiKey !== '') {
+ if ($this->apiKey !== '') {
$defaultHeaders['Authorization'] = 'Bearer ' . $this->apiKey;
}
diff --git a/apps/python-sdk/firecrawl/v2/client.py b/apps/python-sdk/firecrawl/v2/client.py
index b34ce4a38..3b6e3594e 100644
--- a/apps/python-sdk/firecrawl/v2/client.py
+++ b/apps/python-sdk/firecrawl/v2/client.py
@@ -65,6 +65,7 @@ from .methods import extract as extract_module
from .methods import agent as agent_module
from .methods import browser as browser_module
from .methods import monitor as monitor_module
+from .methods import research as research_module
from .watcher import Watcher
# Kwargs that map to ScrapeOptions fields. Used by async crawl normalization
@@ -215,6 +216,21 @@ class FirecrawlClient:
) if any(v is not None for v in [formats, headers, include_tags, exclude_tags, only_main_content, timeout, wait_for, mobile, parsers, actions, location, skip_tls_verification, remove_base64_images, fast_mode, use_mock, block_ads, proxy, max_age, store_in_cache, lockdown, profile, integration]) else None
return scrape_module.scrape(self.http_client, url, options)
+ def search_papers(self, query: str, **kwargs):
+ return research_module.search_papers(self.http_client, query, **kwargs)
+
+ def inspect_paper(self, paper_id: str):
+ return research_module.inspect_paper(self.http_client, paper_id)
+
+ def read_paper(self, paper_id: str, query: str, **kwargs):
+ return research_module.read_paper(self.http_client, paper_id, query, **kwargs)
+
+ def related_papers(self, paper_id: str, intent: str, **kwargs):
+ return research_module.related_papers(self.http_client, paper_id, intent, **kwargs)
+
+ def search_github(self, query: str, **kwargs):
+ return research_module.search_github(self.http_client, query, **kwargs)
+
def interact(
self,
job_id: str,
diff --git a/apps/python-sdk/firecrawl/v2/client_async.py b/apps/python-sdk/firecrawl/v2/client_async.py
index 528b78f4f..20e8184be 100644
--- a/apps/python-sdk/firecrawl/v2/client_async.py
+++ b/apps/python-sdk/firecrawl/v2/client_async.py
@@ -60,6 +60,7 @@ from .methods.aio import extract as async_extract # type: ignore[attr-defined]
from .methods.aio import agent as async_agent # type: ignore[attr-defined]
from .methods.aio import browser as async_browser # type: ignore[attr-defined]
from .methods.aio import monitor as async_monitor # type: ignore[attr-defined]
+from .methods.aio import research as async_research # type: ignore[attr-defined]
from .client import _SCRAPE_OPTION_KEYS
from .watcher_async import AsyncWatcher
@@ -105,6 +106,21 @@ class AsyncFirecrawlClient:
options = ScrapeOptions(**{k: v for k, v in kwargs.items() if v is not None}) if kwargs else None
return await async_scrape.scrape(self.async_http_client, url, options)
+ async def search_papers(self, query: str, **kwargs):
+ return await async_research.search_papers(self.async_http_client, query, **kwargs)
+
+ async def inspect_paper(self, paper_id: str):
+ return await async_research.inspect_paper(self.async_http_client, paper_id)
+
+ async def read_paper(self, paper_id: str, query: str, **kwargs):
+ return await async_research.read_paper(self.async_http_client, paper_id, query, **kwargs)
+
+ async def related_papers(self, paper_id: str, intent: str, **kwargs):
+ return await async_research.related_papers(self.async_http_client, paper_id, intent, **kwargs)
+
+ async def search_github(self, query: str, **kwargs):
+ return await async_research.search_github(self.async_http_client, query, **kwargs)
+
async def interact(
self,
job_id: str,
diff --git a/apps/python-sdk/firecrawl/v2/methods/aio/research.py b/apps/python-sdk/firecrawl/v2/methods/aio/research.py
new file mode 100644
index 000000000..07897c9ba
--- /dev/null
+++ b/apps/python-sdk/firecrawl/v2/methods/aio/research.py
@@ -0,0 +1,117 @@
+"""
+Async research functionality for Firecrawl v2 API.
+"""
+
+from typing import Any, Dict, List, Optional
+from urllib.parse import quote
+
+from ...utils import handle_response_error
+from ...utils.http_client_async import AsyncHttpClient
+from ...utils.get_version import get_version
+
+
+BASE = "/v2/search/research"
+ORIGIN = f"python-sdk@{get_version()}"
+
+
+def _query(params: Dict[str, Any]) -> str:
+ pairs: List[str] = []
+ for key, value in params.items():
+ if value is None:
+ continue
+ values = value if isinstance(value, list) else [value]
+ for item in values:
+ if item is not None:
+ pairs.append(f"{quote(str(key), safe='')}={quote(str(item), safe='')}")
+ return ("?" + "&".join(pairs)) if pairs else ""
+
+
+async def _get(client: AsyncHttpClient, path: str) -> Dict[str, Any]:
+ response = await client.get(path)
+ if response.status_code != 200:
+ handle_response_error(response, "research")
+ return response.json()
+
+
+async def search_papers(
+ client: AsyncHttpClient,
+ query: str,
+ *,
+ k: Optional[int] = None,
+ authors: Optional[List[str]] = None,
+ categories: Optional[List[str]] = None,
+ from_date: Optional[str] = None,
+ to_date: Optional[str] = None,
+) -> Dict[str, Any]:
+ return await _get(
+ client,
+ BASE
+ + "/papers"
+ + _query(
+ {
+ "query": query,
+ "k": k,
+ "authors": authors,
+ "categories": categories,
+ "from": from_date,
+ "to": to_date,
+ "origin": ORIGIN,
+ }
+ ),
+ )
+
+
+async def inspect_paper(client: AsyncHttpClient, paper_id: str) -> Dict[str, Any]:
+ return await _get(client, f"{BASE}/papers/{quote(paper_id, safe='')}")
+
+
+async def read_paper(
+ client: AsyncHttpClient,
+ paper_id: str,
+ query: str,
+ *,
+ k: Optional[int] = None,
+) -> Dict[str, Any]:
+ return await _get(
+ client,
+ f"{BASE}/papers/{quote(paper_id, safe='')}"
+ + _query({"query": query, "k": k, "origin": ORIGIN}),
+ )
+
+
+async def related_papers(
+ client: AsyncHttpClient,
+ paper_id: str,
+ intent: str,
+ *,
+ mode: Optional[str] = None,
+ k: Optional[int] = None,
+ rerank: Optional[bool] = None,
+ anchor: Optional[List[str]] = None,
+) -> Dict[str, Any]:
+ return await _get(
+ client,
+ f"{BASE}/papers/{quote(paper_id, safe='')}/similar"
+ + _query(
+ {
+ "intent": intent,
+ "mode": mode,
+ "k": k,
+ "rerank": None if rerank is None else str(rerank).lower(),
+ "anchor": anchor,
+ "origin": ORIGIN,
+ }
+ ),
+ )
+
+
+async def search_github(
+ client: AsyncHttpClient,
+ query: str,
+ *,
+ k: Optional[int] = None,
+) -> Dict[str, Any]:
+ return await _get(
+ client,
+ BASE + "/github" + _query({"query": query, "k": k, "origin": ORIGIN}),
+ )
diff --git a/apps/python-sdk/firecrawl/v2/methods/research.py b/apps/python-sdk/firecrawl/v2/methods/research.py
new file mode 100644
index 000000000..3255396ac
--- /dev/null
+++ b/apps/python-sdk/firecrawl/v2/methods/research.py
@@ -0,0 +1,116 @@
+"""
+Research functionality for Firecrawl v2 API.
+"""
+
+from typing import Any, Dict, List, Optional
+from urllib.parse import quote
+
+from ..utils import HttpClient, handle_response_error
+from ..utils.get_version import get_version
+
+
+BASE = "/v2/search/research"
+ORIGIN = f"python-sdk@{get_version()}"
+
+
+def _query(params: Dict[str, Any]) -> str:
+ pairs: List[str] = []
+ for key, value in params.items():
+ if value is None:
+ continue
+ values = value if isinstance(value, list) else [value]
+ for item in values:
+ if item is not None:
+ pairs.append(f"{quote(str(key), safe='')}={quote(str(item), safe='')}")
+ return ("?" + "&".join(pairs)) if pairs else ""
+
+
+def _get(client: HttpClient, path: str) -> Dict[str, Any]:
+ response = client.get(path)
+ if response.status_code != 200:
+ handle_response_error(response, "research")
+ return response.json()
+
+
+def search_papers(
+ client: HttpClient,
+ query: str,
+ *,
+ k: Optional[int] = None,
+ authors: Optional[List[str]] = None,
+ categories: Optional[List[str]] = None,
+ from_date: Optional[str] = None,
+ to_date: Optional[str] = None,
+) -> Dict[str, Any]:
+ return _get(
+ client,
+ BASE
+ + "/papers"
+ + _query(
+ {
+ "query": query,
+ "k": k,
+ "authors": authors,
+ "categories": categories,
+ "from": from_date,
+ "to": to_date,
+ "origin": ORIGIN,
+ }
+ ),
+ )
+
+
+def inspect_paper(client: HttpClient, paper_id: str) -> Dict[str, Any]:
+ return _get(client, f"{BASE}/papers/{quote(paper_id, safe='')}")
+
+
+def read_paper(
+ client: HttpClient,
+ paper_id: str,
+ query: str,
+ *,
+ k: Optional[int] = None,
+) -> Dict[str, Any]:
+ return _get(
+ client,
+ f"{BASE}/papers/{quote(paper_id, safe='')}"
+ + _query({"query": query, "k": k, "origin": ORIGIN}),
+ )
+
+
+def related_papers(
+ client: HttpClient,
+ paper_id: str,
+ intent: str,
+ *,
+ mode: Optional[str] = None,
+ k: Optional[int] = None,
+ rerank: Optional[bool] = None,
+ anchor: Optional[List[str]] = None,
+) -> Dict[str, Any]:
+ return _get(
+ client,
+ f"{BASE}/papers/{quote(paper_id, safe='')}/similar"
+ + _query(
+ {
+ "intent": intent,
+ "mode": mode,
+ "k": k,
+ "rerank": None if rerank is None else str(rerank).lower(),
+ "anchor": anchor,
+ "origin": ORIGIN,
+ }
+ ),
+ )
+
+
+def search_github(
+ client: HttpClient,
+ query: str,
+ *,
+ k: Optional[int] = None,
+) -> Dict[str, Any]:
+ return _get(
+ client,
+ BASE + "/github" + _query({"query": query, "k": k, "origin": ORIGIN}),
+ )
diff --git a/apps/ruby-sdk/lib/firecrawl/client.rb b/apps/ruby-sdk/lib/firecrawl/client.rb
index 3bb199d66..d7829677b 100644
--- a/apps/ruby-sdk/lib/firecrawl/client.rb
+++ b/apps/ruby-sdk/lib/firecrawl/client.rb
@@ -85,6 +85,57 @@ module Firecrawl
Models::Document.new(data)
end
+ # Search research papers.
+ #
+ # @param query [String] research query
+ # @param options [Hash] optional query parameters
+ # @return [Hash]
+ def search_papers(query, options = {})
+ @http.get("/v2/search/research/papers#{query(options.merge("query" => query, "origin" => "ruby-sdk@#{Firecrawl::VERSION}"))}")
+ end
+
+ # Inspect paper metadata.
+ #
+ # @param paper_id [String] paper identifier
+ # @return [Hash]
+ def inspect_paper(paper_id)
+ raise ArgumentError, "Paper ID is required" if paper_id.nil?
+ @http.get("/v2/search/research/papers/#{URI.encode_www_form_component(paper_id)}")
+ end
+
+ # Read a paper with query-guided passages.
+ #
+ # @param paper_id [String] paper identifier
+ # @param query_text [String] passage query
+ # @param options [Hash] optional query parameters
+ # @return [Hash]
+ def read_paper(paper_id, query_text, options = {})
+ raise ArgumentError, "Paper ID is required" if paper_id.nil?
+ path = "/v2/search/research/papers/#{URI.encode_www_form_component(paper_id)}"
+ @http.get("#{path}#{query(options.merge("query" => query_text, "origin" => "ruby-sdk@#{Firecrawl::VERSION}"))}")
+ end
+
+ # Find papers related to a paper.
+ #
+ # @param paper_id [String] paper identifier
+ # @param intent [String] relatedness intent
+ # @param options [Hash] optional query parameters
+ # @return [Hash]
+ def related_papers(paper_id, intent, options = {})
+ raise ArgumentError, "Paper ID is required" if paper_id.nil?
+ path = "/v2/search/research/papers/#{URI.encode_www_form_component(paper_id)}/similar"
+ @http.get("#{path}#{query(options.merge("intent" => intent, "origin" => "ruby-sdk@#{Firecrawl::VERSION}"))}")
+ end
+
+ # Search GitHub research content.
+ #
+ # @param query_text [String] GitHub query
+ # @param options [Hash] optional query parameters
+ # @return [Hash]
+ def search_github(query_text, options = {})
+ @http.get("/v2/search/research/github#{query(options.merge("query" => query_text, "origin" => "ruby-sdk@#{Firecrawl::VERSION}"))}")
+ end
+
# Interacts with the scrape-bound browser session for a scrape job.
#
# @param job_id [String] the scrape job ID
@@ -465,9 +516,21 @@ module Firecrawl
private
- def query(**params)
- compact = params.compact
- compact.empty? ? "" : "?#{URI.encode_www_form(compact)}"
+ def query(params = nil, **kwargs)
+ params = (params || {}).merge(kwargs)
+ pairs = []
+ params.each do |key, value|
+ next if value.nil? || value == ""
+
+ values = value.is_a?(Array) ? value : [value]
+ values.each do |item|
+ next if item.nil? || item == ""
+
+ string_value = item == true ? "true" : item == false ? "false" : item.to_s
+ pairs << [key.to_s, string_value]
+ end
+ end
+ pairs.empty? ? "" : "?#{URI.encode_www_form(pairs)}"
end
def poll_crawl(job_id, poll_interval, timeout)
diff --git a/apps/ruby-sdk/test/firecrawl/client_test.rb b/apps/ruby-sdk/test/firecrawl/client_test.rb
index 8771887cd..9581408db 100644
--- a/apps/ruby-sdk/test/firecrawl/client_test.rb
+++ b/apps/ruby-sdk/test/firecrawl/client_test.rb
@@ -15,14 +15,14 @@ class ClientTest < Minitest::Test
# CLIENT INITIALIZATION
# ================================================================
- def test_raises_when_no_api_key
+ def test_allows_no_api_key_for_keyless_endpoints
ENV.delete("FIRECRAWL_API_KEY")
- assert_raises(Firecrawl::FirecrawlError) { Firecrawl::Client.new }
+ assert_instance_of Firecrawl::Client, Firecrawl::Client.new
end
- def test_raises_when_whitespace_only_api_key
+ def test_allows_whitespace_only_api_key_for_keyless_endpoints
ENV.delete("FIRECRAWL_API_KEY")
- assert_raises(Firecrawl::FirecrawlError) { Firecrawl::Client.new(api_key: " ") }
+ assert_instance_of Firecrawl::Client, Firecrawl::Client.new(api_key: " ")
end
def test_raises_when_api_url_not_http
@@ -69,7 +69,7 @@ class ClientTest < Minitest::Test
def test_scrape_basic
stub_request(:post, "#{BASE_URL}/v2/scrape")
.with(
- body: { url: "https://example.com" }.to_json,
+ body: { url: "https://example.com", origin: "ruby-sdk@#{Firecrawl::VERSION}" }.to_json,
headers: { "Authorization" => "Bearer #{API_KEY}", "Content-Type" => "application/json" }
)
.to_return(
diff --git a/apps/rust-sdk/src/lib.rs b/apps/rust-sdk/src/lib.rs
index 4639f0cdb..d71a0bd97 100644
--- a/apps/rust-sdk/src/lib.rs
+++ b/apps/rust-sdk/src/lib.rs
@@ -27,6 +27,7 @@ mod crawl;
mod map;
mod monitor;
mod parse;
+mod research;
mod scrape;
mod search;
mod types;
@@ -39,6 +40,7 @@ pub use error::FirecrawlError;
pub use map::*;
pub use monitor::*;
pub use parse::*;
+pub use research::*;
pub use scrape::*;
pub use search::*;
pub use types::*;
diff --git a/apps/rust-sdk/src/research.rs b/apps/rust-sdk/src/research.rs
new file mode 100644
index 000000000..232c523eb
--- /dev/null
+++ b/apps/rust-sdk/src/research.rs
@@ -0,0 +1,321 @@
+//! Research endpoints for Firecrawl API v2.
+
+use serde::{Deserialize, Serialize};
+use serde_json::Value;
+use std::collections::HashMap;
+
+use crate::client::Client;
+use crate::FirecrawlError;
+
+#[serde_with::skip_serializing_none]
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+#[serde(rename_all = "camelCase")]
+pub struct PaperResult {
+ pub paper_id: Option<String>,
+ pub primary_id: Option<String>,
+ pub ids: Option<HashMap<String, Value>>,
+ pub title: Option<String>,
+ #[serde(rename = "abstract")]
+ pub abstract_text: Option<String>,
+ pub score: Option<f64>,
+ pub year: Option<i32>,
+ pub authors: Option<Vec<String>>,
+ pub venue: Option<String>,
+ pub url: Option<String>,
+ pub signals: Option<HashMap<String, Value>>,
+}
+
+#[serde_with::skip_serializing_none]
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+#[serde(rename_all = "camelCase")]
+pub struct PaperMetadata {
+ pub paper_id: Option<String>,
+ pub ids: Option<HashMap<String, Value>>,
+ pub title: Option<String>,
+ #[serde(rename = "abstract")]
+ pub abstract_text: Option<String>,
+ pub authors: Option<String>,
+ pub categories: Option<Vec<String>>,
+ pub created_date: Option<String>,
+ pub update_date: Option<String>,
+}
+
+#[serde_with::skip_serializing_none]
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+pub struct Passage {
+ pub text: Option<String>,
+ pub section: Option<String>,
+ pub page: Option<i32>,
+ pub score: Option<f64>,
+ pub metadata: Option<HashMap<String, Value>>,
+}
+
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+pub struct SearchPapersResponse {
+ pub success: bool,
+ pub results: Vec<PaperResult>,
+}
+
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+pub struct PaperMetadataResponse {
+ pub success: bool,
+ pub paper: PaperMetadata,
+}
+
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+#[serde(rename_all = "camelCase")]
+pub struct ReadPaperResponse {
+ pub success: bool,
+ pub paper: PaperMetadata,
+ pub paper_id: Option<String>,
+ pub query: Option<String>,
+ pub passages: Option<Vec<Passage>>,
+}
+
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+#[serde(rename_all = "camelCase")]
+pub struct SimilarPapersResponse {
+ pub success: bool,
+ pub results: Vec<PaperResult>,
+ pub pool_size: Option<i32>,
+ pub truncated: bool,
+ pub note: Option<String>,
+}
+
+#[serde_with::skip_serializing_none]
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+#[serde(rename_all = "camelCase")]
+pub struct GitHubSearchItem {
+ pub result_type: Option<String>,
+ pub repo: Option<String>,
+ pub url: Option<String>,
+ pub page_type: Option<String>,
+ pub number: Option<i32>,
+ pub segment_count: Option<i32>,
+ pub readme_url: Option<String>,
+ pub title: Option<String>,
+ pub snippet: Option<String>,
+ pub content_md: Option<String>,
+ pub scores: Option<HashMap<String, Value>>,
+}
+
+#[derive(Deserialize, Serialize, Debug, Default, Clone)]
+pub struct GitHubSearchResponse {
+ pub success: bool,
+ pub results: Vec<GitHubSearchItem>,
+}
+
+#[derive(Debug, Default, Clone)]
+pub struct SearchPapersOptions {
+ pub k: Option<u32>,
+ pub authors: Option<Vec<String>>,
+ pub categories: Option<Vec<String>>,
+ pub from: Option<String>,
+ pub to: Option<String>,
+}
+
+#[derive(Debug, Default, Clone)]
+pub struct ReadPaperOptions {
+ pub k: Option<u32>,
+}
+
+#[derive(Debug, Default, Clone)]
+pub struct RelatedPapersOptions {
+ pub mode: Option<String>,
+ pub k: Option<u32>,
+ pub rerank: Option<bool>,
+ pub anchor: Option<Vec<String>>,
+}
+
+#[derive(Debug, Default, Clone)]
+pub struct SearchGitHubOptions {
+ pub k: Option<u32>,
+}
+
+fn push_query(query: &mut Vec<(String, String)>, key: &str, value: impl ToString) {
+ query.push((key.to_string(), value.to_string()));
+}
+
+fn path_escape(value: &str) -> String {
+ const HEX: &[u8; 16] = b"0123456789ABCDEF";
+ let mut out = String::with_capacity(value.len());
+ for byte in value.bytes() {
+ if byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.' | b'~') {
+ out.push(byte as char);
+ } else {
+ out.push('%');
+ out.push(HEX[(byte >> 4) as usize] as char);
+ out.push(HEX[(byte & 0x0f) as usize] as char);
+ }
+ }
+ out
+}
+
+impl Client {
+ pub async fn search_papers(
+ &self,
+ query_text: impl AsRef<str>,
+ options: impl Into<Option<SearchPapersOptions>>,
+ ) -> Result<SearchPapersResponse, FirecrawlError> {
+ let mut query = vec![
+ ("query".to_string(), query_text.as_ref().to_string()),
+ (
+ "origin".to_string(),
+ format!("rust-sdk@{}", env!("CARGO_PKG_VERSION")),
+ ),
+ ];
+ if let Some(options) = options.into() {
+ if let Some(k) = options.k {
+ push_query(&mut query, "k", k);
+ }
+ for author in options.authors.unwrap_or_default() {
+ push_query(&mut query, "authors", author);
+ }
+ for category in options.categories.unwrap_or_default() {
+ push_query(&mut query, "categories", category);
+ }
+ if let Some(from) = options.from {
+ push_query(&mut query, "from", from);
+ }
+ if let Some(to) = options.to {
+ push_query(&mut query, "to", to);
+ }
+ }
+
+ let response = self
+ .client
+ .get(self.url("/search/research/papers"))
+ .headers(self.prepare_headers(None))
+ .query(&query)
+ .send()
+ .await
+ .map_err(|e| FirecrawlError::HttpError("search papers".to_string(), e))?;
+
+ self.handle_response(response, "search papers").await
+ }
+
+ pub async fn inspect_paper(
+ &self,
+ paper_id: impl AsRef<str>,
+ ) -> Result<PaperMetadataResponse, FirecrawlError> {
+ let response = self
+ .client
+ .get(self.url(&format!(
+ "/search/research/papers/{}",
+ path_escape(paper_id.as_ref())
+ )))
+ .headers(self.prepare_headers(None))
+ .send()
+ .await
+ .map_err(|e| FirecrawlError::HttpError("inspect paper".to_string(), e))?;
+
+ self.handle_response(response, "inspect paper").await
+ }
+
+ pub async fn read_paper(
+ &self,
+ paper_id: impl AsRef<str>,
+ query_text: impl AsRef<str>,
+ options: impl Into<Option<ReadPaperOptions>>,
+ ) -> Result<ReadPaperResponse, FirecrawlError> {
+ let mut query = vec![
+ ("query".to_string(), query_text.as_ref().to_string()),
+ (
+ "origin".to_string(),
+ format!("rust-sdk@{}", env!("CARGO_PKG_VERSION")),
+ ),
+ ];
+ if let Some(options) = options.into() {
+ if let Some(k) = options.k {
+ push_query(&mut query, "k", k);
+ }
+ }
+
+ let response = self
+ .client
+ .get(self.url(&format!(
+ "/search/research/papers/{}",
+ path_escape(paper_id.as_ref())
+ )))
+ .headers(self.prepare_headers(None))
+ .query(&query)
+ .send()
+ .await
+ .map_err(|e| FirecrawlError::HttpError("read paper".to_string(), e))?;
+
+ self.handle_response(response, "read paper").await
+ }
+
+ pub async fn related_papers(
+ &self,
+ paper_id: impl AsRef<str>,
+ intent: impl AsRef<str>,
+ options: impl Into<Option<RelatedPapersOptions>>,
+ ) -> Result<SimilarPapersResponse, FirecrawlError> {
+ let mut query = vec![
+ ("intent".to_string(), intent.as_ref().to_string()),
+ (
+ "origin".to_string(),
+ format!("rust-sdk@{}", env!("CARGO_PKG_VERSION")),
+ ),
+ ];
+ if let Some(options) = options.into() {
+ if let Some(mode) = options.mode {
+ push_query(&mut query, "mode", mode);
+ }
+ if let Some(k) = options.k {
+ push_query(&mut query, "k", k);
+ }
+ if let Some(rerank) = options.rerank {
+ push_query(&mut query, "rerank", rerank);
+ }
+ for anchor in options.anchor.unwrap_or_default() {
+ push_query(&mut query, "anchor", anchor);
+ }
+ }
+
+ let response = self
+ .client
+ .get(self.url(&format!(
+ "/search/research/papers/{}/similar",
+ path_escape(paper_id.as_ref())
+ )))
+ .headers(self.prepare_headers(None))
+ .query(&query)
+ .send()
+ .await
+ .map_err(|e| FirecrawlError::HttpError("related papers".to_string(), e))?;
+
+ self.handle_response(response, "related papers").await
+ }
+
+ pub async fn search_github(
+ &self,
+ query_text: impl AsRef<str>,
+ options: impl Into<Option<SearchGitHubOptions>>,
+ ) -> Result<GitHubSearchResponse, FirecrawlError> {
+ let mut query = vec![
+ ("query".to_string(), query_text.as_ref().to_string()),
+ (
+ "origin".to_string(),
+ format!("rust-sdk@{}", env!("CARGO_PKG_VERSION")),
+ ),
+ ];
+ if let Some(options) = options.into() {
+ if let Some(k) = options.k {
+ push_query(&mut query, "k", k);
+ }
+ }
+
+ let response = self
+ .client
+ .get(self.url("/search/research/github"))
+ .headers(self.prepare_headers(None))
+ .query(&query)
+ .send()
+ .await
+ .map_err(|e| FirecrawlError::HttpError("search github".to_string(), e))?;
+
+ self.handle_response(response, "search github").await
+ }
+}