Skip to content

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

feat(api): safe param on v2 /search (#4251)

Rafael Miller44bf8f1f10bf
diff --git a/apps/api/src/__tests__/snips/v2/search.test.ts b/apps/api/src/__tests__/snips/v2/search.test.ts
index 60fdc5658..5eebd963b 100644
--- a/apps/api/src/__tests__/snips/v2/search.test.ts
+++ b/apps/api/src/__tests__/snips/v2/search.test.ts
@@ -260,6 +260,38 @@ describeIf(TEST_PRODUCTION || HAS_SEARCH || HAS_PROXY)("Search tests", () => {
60000,
);
+ concurrentIf(TEST_PRODUCTION)(
+ "works with safe",
+ async () => {
+ const res = await search(
+ {
+ query: "firecrawl",
+ safe: true,
+ },
+ identity,
+ );
+ expect(res.web).toBeDefined();
+ expect(res.web?.length).toBeGreaterThan(0);
+ },
+ 60000,
+ );
+
+ it.concurrent(
+ "rejects non-boolean safe",
+ async () => {
+ const res = await searchWithFailure(
+ {
+ query: "firecrawl",
+ safe: "active",
+ } as any,
+ identity,
+ );
+
+ expect(res.error).toBe("Invalid request body");
+ },
+ 60000,
+ );
+
it.concurrent(
"country defaults to undefined when location is set",
async () => {
diff --git a/apps/api/src/controllers/v2/search.ts b/apps/api/src/controllers/v2/search.ts
index 63e08aef2..0cfaca21b 100644
--- a/apps/api/src/controllers/v2/search.ts
+++ b/apps/api/src/controllers/v2/search.ts
@@ -213,9 +213,9 @@ export async function searchController(
);
if (!reservation.ok) {
applyAgentAuthDiscoveryHeader(res);
- return res.status(429).json(
- await keylessLimitBody(req.auth.team_id, "v2_search"),
- );
+ return res
+ .status(429)
+ .json(await keylessLimitBody(req.auth.team_id, "v2_search"));
}
reservedKeylessCredits = projectedKeylessCredits;
}
@@ -229,6 +229,7 @@ export async function searchController(
lang: req.body.lang,
country: req.body.country,
location: req.body.location,
+ safe: req.body.safe,
sources: req.body.sources as Array<{ type: string }>,
categories: req.body.categories as CategoryOption[],
includeDomains: req.body.includeDomains,
diff --git a/apps/api/src/controllers/v2/types.ts b/apps/api/src/controllers/v2/types.ts
index 4a1dc62f5..225d7fcf0 100644
--- a/apps/api/src/controllers/v2/types.ts
+++ b/apps/api/src/controllers/v2/types.ts
@@ -2029,6 +2029,7 @@ export const searchRequestSchema = z
includeDomains: z.array(searchDomainSchema).optional(),
excludeDomains: z.array(searchDomainSchema).optional(),
lang: z.string().optional().prefault("en"),
+ safe: z.boolean().optional(),
enterprise: z.array(z.enum(["default", "anon", "zdr"])).optional(),
country: z.string().optional(),
location: z.string().optional(),
diff --git a/apps/api/src/search/execute.ts b/apps/api/src/search/execute.ts
index afd6dc51f..8a74a59bf 100644
--- a/apps/api/src/search/execute.ts
+++ b/apps/api/src/search/execute.ts
@@ -35,6 +35,7 @@ interface SearchOptions {
lang?: string;
country?: string;
location?: string;
+ safe?: boolean;
sources: Array<{ type: string }>;
categories?: CategoryOption[];
includeDomains?: string[];
@@ -122,6 +123,7 @@ export async function executeSearch(
lang: options.lang,
country: options.country,
location: options.location,
+ safe: options.safe,
type: searchTypes,
enterprise: options.enterprise,
})) as SearchV2Response;
diff --git a/apps/api/src/search/v2/fireEngine-v2.ts b/apps/api/src/search/v2/fireEngine-v2.ts
index 708574054..b5b174b08 100644
--- a/apps/api/src/search/v2/fireEngine-v2.ts
+++ b/apps/api/src/search/v2/fireEngine-v2.ts
@@ -45,6 +45,7 @@ export async function fire_engine_search_v2(
lang?: string;
country?: string;
location?: string;
+ safe?: boolean;
numResults: number;
page?: number;
type?: SearchResultType | SearchResultType[];
@@ -69,6 +70,7 @@ export async function fire_engine_search_v2(
page: options.page ?? 1,
type: options.type || "web",
enterprise: options.enterprise,
+ safe: options.safe ? ("active" as const) : undefined,
};
const requestedTypes = normalizeSearchTypes(options.type);
diff --git a/apps/api/src/search/v2/index.ts b/apps/api/src/search/v2/index.ts
index 3dca1a848..271e5d431 100644
--- a/apps/api/src/search/v2/index.ts
+++ b/apps/api/src/search/v2/index.ts
@@ -15,6 +15,7 @@ export async function search({
lang = "en",
country = "us",
location = undefined,
+ safe = undefined,
proxy = undefined,
sleep_interval = 0,
timeout = 5000,
@@ -30,6 +31,7 @@ export async function search({
lang?: string;
country?: string;
location?: string;
+ safe?: boolean;
proxy?: string;
sleep_interval?: number;
timeout?: number;
@@ -46,6 +48,7 @@ export async function search({
lang,
country,
location,
+ safe,
type,
enterprise,
});
@@ -62,6 +65,7 @@ export async function search({
lang,
country,
location,
+ safe,
});
if (results.web && results.web.length > 0) return results;
}
diff --git a/apps/api/src/search/v2/searxng.ts b/apps/api/src/search/v2/searxng.ts
index 445c007ed..16661a859 100644
--- a/apps/api/src/search/v2/searxng.ts
+++ b/apps/api/src/search/v2/searxng.ts
@@ -9,6 +9,7 @@ interface SearchOptions {
lang?: string;
country?: string;
location?: string;
+ safe?: boolean;
num_results: number;
page?: number;
}
@@ -36,6 +37,8 @@ export async function searxng_search(
categories: config.SEARXNG_CATEGORIES ?? "",
pageno: page,
format: "json",
+ // 2 = strict; omitted otherwise so the instance default applies
+ ...(options.safe ? { safesearch: 2 } : {}),
};
const response = await axios.get(finalUrl, {