Skip to content

Open source alternative to Semrush and Ahrefs

Treat DataForSEO HTTP 403 as a credentials failure (#1032)

Ben Senescu91e60885239e
diff --git a/src/client/lib/error-messages.ts b/src/client/lib/error-messages.ts
index 524afc3b..15408f48 100644
--- a/src/client/lib/error-messages.ts
+++ b/src/client/lib/error-messages.ts
@@ -38,7 +38,7 @@ const STANDARD_MESSAGES: Record<ErrorCode, string> = {
AI_VISIBILITY_ERROR:
"The AI visibility request could not be completed. Review the tracking settings and try again.",
DATAFORSEO_AUTH_FAILED:
- "DataForSEO rejected the API key. Check that DATAFORSEO_API_KEY is the base64 of your DataForSEO login:password.",
+ "DataForSEO rejected the credentials. Check that DATAFORSEO_API_KEY is the base64 of your DataForSEO login:password, and that the DataForSEO account is verified and has API access.",
RATE_LIMITED: "Too many requests. Please wait and try again.",
UPSTREAM_UNAVAILABLE:
"The data provider is temporarily unavailable. Please retry in a moment.",
diff --git a/src/server/lib/dataforseo/core.test.ts b/src/server/lib/dataforseo/core.test.ts
index cb5ff7e3..8b19424f 100644
--- a/src/server/lib/dataforseo/core.test.ts
+++ b/src/server/lib/dataforseo/core.test.ts
@@ -30,6 +30,22 @@ describe("DataForSEO transport", () => {
);
});
+ // An unverified account answers with HTTP 403 (status_code 40104). Left as
+ // INTERNAL_ERROR it reached self-hosters as "an unexpected error occurred".
+ it("maps a 403 to DATAFORSEO_AUTH_FAILED", async () => {
+ const fetchMock = vi
+ .fn<typeof fetch>()
+ .mockResolvedValue(
+ Response.json({ status_code: 40104 }, { status: 403 }),
+ );
+ vi.stubGlobal("fetch", fetchMock);
+
+ await expect(
+ dataforseoPost("/v3/backlinks/summary/live", []),
+ ).rejects.toMatchObject({ code: "DATAFORSEO_AUTH_FAILED" });
+ expect(fetchMock).toHaveBeenCalledOnce();
+ });
+
// The request-deadline abort arrives as a bare DOMException. Left unclassified
// it escapes as an anonymous INTERNAL_ERROR; retrying it would replay a call
// DataForSEO may already have billed. Both names are reachable: the shared
diff --git a/src/server/lib/dataforseo/core.ts b/src/server/lib/dataforseo/core.ts
index 8adff411..0ed86dcf 100644
--- a/src/server/lib/dataforseo/core.ts
+++ b/src/server/lib/dataforseo/core.ts
@@ -119,12 +119,13 @@ function createAuthenticatedFetch(
const classified = classify?.(response.status, rawText, path);
if (classified) throw classified;
+ // An unverified account (status_code 40104) answers with 403, not 401.
const code: ErrorCode =
response.status >= 500
? "UPSTREAM_UNAVAILABLE"
: response.status === 429
? "RATE_LIMITED"
- : response.status === 401
+ : response.status === 401 || response.status === 403
? "DATAFORSEO_AUTH_FAILED"
: "INTERNAL_ERROR";
const error = new AppError(