Skip to content

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

feat(agent-signup): integrate qs for query string handling and refactor sponsor record insertion

Erik Engervall0a79e06bc039
diff --git a/apps/api/package.json b/apps/api/package.json
index 023831b50..bd162867e 100644
--- a/apps/api/package.json
+++ b/apps/api/package.json
@@ -56,6 +56,7 @@
"@types/node": "^22.19.1",
"@types/pdf-parse": "^1.1.4",
"@types/pg": "^8.15.5",
+ "@types/qs": "^6.14.0",
"@types/supertest": "^6.0.2",
"@types/tough-cookie": "^4.0.5",
"husky": "^9.1.7",
@@ -127,6 +128,7 @@
"prettier": "^3.6.2",
"prom-client": "^15.1.3",
"psl": "^1.15.0",
+ "qs": "^6.15.0",
"rate-limiter-flexible": "2.4.2",
"redlock": "5.0.0-beta.2",
"resend": "^3.5.0",
diff --git a/apps/api/pnpm-lock.yaml b/apps/api/pnpm-lock.yaml
index 0a7a7d9e5..3e55f106c 100644
--- a/apps/api/pnpm-lock.yaml
+++ b/apps/api/pnpm-lock.yaml
@@ -189,6 +189,9 @@ importers:
psl:
specifier: ^1.15.0
version: 1.15.0
+ qs:
+ specifier: ^6.15.0
+ version: 6.15.0
rate-limiter-flexible:
specifier: 2.4.2
version: 2.4.2
@@ -280,6 +283,9 @@ importers:
'@types/pg':
specifier: ^8.15.5
version: 8.15.5
+ '@types/qs':
+ specifier: ^6.14.0
+ version: 6.14.0
'@types/supertest':
specifier: ^6.0.2
version: 6.0.2
@@ -2678,8 +2684,8 @@ packages:
resolution: {integrity: sha512-PIzZZlEppgrpoT2QgbnDU+MMzuR6BbCjllj0bM70lWoejMeNJAxCchxnv7J3XFkI8MpygtRpzXrIlmWUBclP5A==}
- '@types/[email protected]':
- resolution: {integrity: sha512-uXHQKES6DQKKCLh441Xv/dwxOq1TVS3JPUMlEqoEglvlhR6Mxnlew/Xq/LRVHpLyk7iK3zODe1qYHIMltO7XGg==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-eOunJqu0K1923aExK6y8p6fsihYEn/BYuQ4g0CxAAgFc4b/ZLN4CrsRZ55srTdqoiLzU2B2evC+apEIxprEzkQ==}
resolution: {integrity: sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==}
@@ -4888,8 +4894,8 @@ packages:
resolution: {integrity: sha512-oTUZM/NAZS8p7ANR3SHh30kXB+zK2r2BPcEn/awJIbOvq82WoMN4p62AWWp3Hhw50G0xMsw1mhIBLqHw64EcNQ==}
- resolution: {integrity: sha512-V/yCWTTF7VJ9hIh18Ugr2zhJMP01MY7c5kh4J870L7imm6/DIzBsNLTXzMwUA3yZ5b/KBqLx8Kp3uRvd7xSe3Q==}
+ resolution: {integrity: sha512-mAZTtNCeetKMH+pSjrb76NAM8V9a05I9aBZOHztWy/UqcJdQYNsf59vrRKWnojAT9Y+GbIvoTBC++CPHqpDBhQ==}
engines: {node: '>=0.6'}
@@ -8145,7 +8151,7 @@ snapshots:
dependencies:
'@types/node': 24.10.1
- '@types/qs': 6.9.15
+ '@types/qs': 6.14.0
'@types/range-parser': 1.2.7
'@types/send': 0.17.4
@@ -8159,7 +8165,7 @@ snapshots:
dependencies:
'@types/body-parser': 1.19.5
'@types/express-serve-static-core': 4.19.3
- '@types/qs': 6.9.15
+ '@types/qs': 6.14.0
'@types/serve-static': 1.15.7
'@types/[email protected]': {}
@@ -8225,7 +8231,7 @@ snapshots:
'@types/[email protected]': {}
- '@types/[email protected]': {}
+ '@types/[email protected]': {}
'@types/[email protected]': {}
@@ -8627,7 +8633,7 @@ snapshots:
http-errors: 2.0.0
iconv-lite: 0.4.24
on-finished: 2.4.1
- qs: 6.14.2
+ qs: 6.15.0
raw-body: 2.5.2
type-is: 1.6.18
unpipe: 1.0.0
@@ -9204,7 +9210,7 @@ snapshots:
parseurl: 1.3.3
path-to-regexp: 0.1.12
proxy-addr: 2.0.7
- qs: 6.14.2
+ qs: 6.15.0
range-parser: 1.2.1
safe-buffer: 5.2.1
send: 0.19.0
@@ -9344,7 +9350,7 @@ snapshots:
'@paralleldrive/cuid2': 2.2.2
dezalgo: 1.0.4
once: 1.4.0
- qs: 6.14.2
+ qs: 6.15.0
@@ -10765,7 +10771,7 @@ snapshots:
dependencies:
side-channel: 1.1.0
@@ -11135,7 +11141,7 @@ snapshots:
dependencies:
'@types/node': 24.10.1
- qs: 6.14.2
+ qs: 6.15.0
@@ -11151,7 +11157,7 @@ snapshots:
formidable: 2.1.5
methods: 1.1.2
mime: 2.6.0
- qs: 6.14.2
+ qs: 6.15.0
semver: 7.6.2
transitivePeerDependencies:
- supports-color
diff --git a/apps/api/src/controllers/v2/agent-signup.ts b/apps/api/src/controllers/v2/agent-signup.ts
index 4a60e51c2..5e5502367 100644
--- a/apps/api/src/controllers/v2/agent-signup.ts
+++ b/apps/api/src/controllers/v2/agent-signup.ts
@@ -1,5 +1,6 @@
import crypto from "crypto";
import { Request, Response } from "express";
+import qs from "qs";
import { RateLimiterRedis } from "rate-limiter-flexible";
import { Resend } from "resend";
import { z } from "zod";
@@ -53,6 +54,21 @@ const agentSignupSchema = z.object({
accept_terms: z.literal(true),
});
+/** Insert payload for agent_sponsors (nullable cols in DB are optional here). */
+type AgentSponsorInsert = {
+ email: string;
+ status: "pending" | "blocked" | "verified";
+ verification_deadline: string;
+ agent_name: string;
+ sandboxed_team_id: string;
+ api_key_id: number;
+ verification_token: string;
+
+ requesting_ip?: string;
+ tos_version?: string;
+ tos_hash?: string;
+};
+
export async function agentSignupController(req: Request, res: Response) {
const logger = _logger.child({
module: "v2/agent-signup",
@@ -196,6 +212,12 @@ export async function agentSignupController(req: Request, res: Response) {
apiKeyRecord = apiKeyData;
+ if (!teamId) {
+ return res
+ .status(500)
+ .json({ success: false, error: "Failed to create agent account." });
+ }
+
// Mark the API key as agent_provisioned
const { error: updateKeyError } = await supabase_service
.from("api_keys")
@@ -216,23 +238,24 @@ export async function agentSignupController(req: Request, res: Response) {
deadline.setDate(deadline.getDate() + 3);
// Create sponsor record
+ const sponsorRow: AgentSponsorInsert = {
+ email: email.toLowerCase(),
+ status: "pending",
+ verification_deadline: deadline.toISOString(),
+ agent_name,
+ sandboxed_team_id: teamId,
+ api_key_id: apiKeyRecord.id,
+ requesting_ip: incomingIP,
+ tos_version: "2025-01-01",
+ tos_hash: crypto
+ .createHash("sha256")
+ .update("accept_terms:true")
+ .digest("hex"),
+ verification_token: verificationToken,
+ };
const { error: sponsorError } = await supabase_service
.from("agent_sponsors")
- .insert({
- email: email.toLowerCase(),
- status: "pending",
- verification_deadline: deadline.toISOString(),
- agent_name,
- sandboxed_team_id: teamId,
- api_key_id: apiKeyRecord.id,
- requesting_ip: incomingIP,
- tos_version: "2025-01-01",
- tos_hash: crypto
- .createHash("sha256")
- .update("accept_terms:true")
- .digest("hex"),
- verification_token: verificationToken,
- } as any);
+ .insert(sponsorRow);
if (sponsorError) {
logger.error("Failed to create sponsor record", { error: sponsorError });
@@ -242,8 +265,9 @@ export async function agentSignupController(req: Request, res: Response) {
}
// Send confirmation email
- const confirmUrl = `https://firecrawl.dev/agent-confirm?token=${verificationToken}&action=confirm`;
- const blockUrl = `https://firecrawl.dev/agent-confirm?token=${verificationToken}&action=block`;
+ const baseUrl = "https://firecrawl.dev/agent-confirm";
+ const confirmUrl = `${baseUrl}?${qs.stringify({ token: verificationToken, action: "confirm" })}`;
+ const blockUrl = `${baseUrl}?${qs.stringify({ token: verificationToken, action: "block" })}`;
if (config.RESEND_API_KEY) {
const resend = new Resend(config.RESEND_API_KEY);
diff --git a/apps/api/src/services/agent-sponsor.ts b/apps/api/src/services/agent-sponsor.ts
index f2f9bbbab..bfdda9f16 100644
--- a/apps/api/src/services/agent-sponsor.ts
+++ b/apps/api/src/services/agent-sponsor.ts
@@ -1,6 +1,6 @@
-import { supabase_service, supabase_rr_service } from "./supabase";
-import { getValue, setValue, deleteKey } from "./redis";
import { logger } from "../lib/logger";
+import { deleteKey, getValue, setValue } from "./redis";
+import { supabase_rr_service, supabase_service } from "./supabase";
type AgentSponsorStatus = {
status: "pending" | "verified" | "blocked";
@@ -11,8 +11,13 @@ type AgentSponsorStatus = {
verification_token: string;
};
+/** Value stored in Redis: either sponsor data or a sentinel for "no sponsor". */
+type AgentSponsorCacheValue = AgentSponsorStatus | { _none: true };
+
const AGENT_SPONSOR_CACHE_TTL = 300; // 5 minutes
+const CACHE_MISS_SENTINEL: AgentSponsorCacheValue = { _none: true };
+
/**
* Look up agent sponsor status by api_key_id with Redis caching.
*/
@@ -21,12 +26,16 @@ export async function getAgentSponsorStatus(
): Promise<AgentSponsorStatus | null> {
const cacheKey = `agent_sponsor_${api_key_id}`;
- const cached = await getValue(cacheKey);
+ const cached: string | null = await getValue(cacheKey);
if (cached !== null) {
- const parsed = JSON.parse(cached);
- // Cache "no sponsor" as empty object
- if (parsed && parsed._none) return null;
- return parsed;
+ try {
+ const parsed = JSON.parse(cached) as AgentSponsorCacheValue;
+ // Cache "no sponsor" as empty object
+ if (parsed && "_none" in parsed && parsed._none) return null;
+ return parsed as AgentSponsorStatus;
+ } catch {
+ // Corrupt cache: fall through to DB lookup
+ }
}
try {
@@ -42,7 +51,7 @@ export async function getAgentSponsorStatus(
// Cache the miss to avoid repeated queries
await setValue(
cacheKey,
- JSON.stringify({ _none: true }),
+ JSON.stringify(CACHE_MISS_SENTINEL),
AGENT_SPONSOR_CACHE_TTL,
);
return null;