earendil-works/pi · error · Error
Token exchange returned invalid JSON. url=${TOKEN_URL}; body
Error message
Token exchange returned invalid JSON. url=${TOKEN_URL}; body=${responseBody}; details=${formatErrorDetails(error)} What it means
Error "Token exchange returned invalid JSON. url=${TOKEN_URL}; body=${responseBody}; details=${formatErrorDetails(error)}" thrown in earendil-works/pi.
Source
Thrown at packages/ai/src/auth/oauth/anthropic.ts:221
client_id: CLIENT_ID,
code,
state,
redirect_uri: redirectUri,
code_verifier: verifier,
},
signal,
);
} catch (error) {
throw new Error(
`Token exchange request failed. url=${TOKEN_URL}; redirect_uri=${redirectUri}; response_type=authorization_code; details=${formatErrorDetails(error)}`,
);
}
let tokenData: { access_token: string; refresh_token: string; expires_in: number };
try {
tokenData = JSON.parse(responseBody) as { access_token: string; refresh_token: string; expires_in: number };
} catch (error) {
throw new Error(
`Token exchange returned invalid JSON. url=${TOKEN_URL}; body=${responseBody}; details=${formatErrorDetails(error)}`,
);
}
return {
type: "oauth",
refresh: tokenData.refresh_token,
access: tokenData.access_token,
expires: Date.now() + tokenData.expires_in * 1000 - 5 * 60 * 1000,
};
}
async function loginAnthropic(interaction: ProviderAuthInteraction): Promise<OAuthCredential> {
const { verifier, challenge } = await generatePKCE();
const server = await startCallbackServer(verifier);
const manualAbort = new AbortController();
const onAbort = () => server.cancelWait();
interaction.signal.addEventListener("abort", onAbort, { once: true });View on GitHub (pinned to 4af9d21d3b)
Solutions
- Inspect the response body; the token endpoint returned non-JSON, so retry or check the endpoint.
When it happens
Trigger: Thrown at packages/ai/src/auth/oauth/anthropic.ts:221 when the library encounters an invalid state.
Common situations: See trigger scenarios.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
AI-assisted analysis of earendil-works/pi@4af9d21d3b (2026-08-24).
Data as JSON: /api/errors/59bc1b71cb70cfe9.
Report an issue: GitHub.