{"slug": "relayrouter-api-troubleshooting-why-claude-code-gets-html-instead-of-json", "title": "RelayRouter API Troubleshooting: Why Claude Code Gets HTML Instead of JSON", "summary": "A developer documented a debugging method for RelayRouter API failures where Claude Code and OpenAI-compatible SDKs receive HTML instead of JSON, producing the error 'Unexpected token `<` in JSON at position 0'. The writeup advises checking the exact requested URL, status code, and Content-Type header before involving an SDK, and notes that Claude Code requires an Anthropic Messages-compatible route such as /v1/messages rather than an OpenAI-compatible /v1/chat/completions endpoint alone.", "body_md": "The error looks like a JSON problem:\n\nUnexpected token `<` in JSON at position 0\n\nBut the `<` is usually the clue. Your client expected an API response and received an HTML page instead.\n\nI start debugging this by asking one question: **What exact URL did the client request?**\n\nA gateway can serve a website, a dashboard, and an API from related addresses. A valid domain does not necessarily point to a valid API route. There is also a second trap: Claude Code and an OpenAI-compatible SDK do not speak the same API protocol.\n\nFor an OpenAI-compatible **Chat Completions** client, the RelayRouter quickstart documents this base URL:\n\n```\nhttps://api.relayrouter.ai/v1\n```\n\nThe client then calls:\n\n```\nPOST https://api.relayrouter.ai/v1/chat/completions\n```\n\nClaude Code is different. It expects an **Anthropic Messages-compatible** gateway, including a route such as `/v1/messages`. An OpenAI-compatible `/v1/chat/completions` endpoint alone is insufficient. Check that the gateway explicitly supports the Anthropic Messages protocol before configuring Claude Code.\n\nThose are two separate checks:\n\nImagine configuring a client with a site URL:\n\n```\nhttps://example-gateway.com\n```\n\nThat address may serve a landing page. If the client sends a request to a path the site does not recognize, the server or a reverse proxy may return an HTML error page. The SDK tries to parse that page as JSON, reaches the first `<` in `<!doctype html>`, and fails.\n\nOther possible causes include a login redirect, a stale hostname, a missing API prefix, or a proxy that replaces an upstream error with its own HTML page.\n\nThe JSON parser is reporting the symptom. The response’s **status code, content type, and final URL** tell you what happened.\n\nHere is a minimal request to an OpenAI-compatible Chat Completions route. Replace `YOUR_MODEL_ID` with a model ID from your current dashboard.\n\n```\ncurl -i -sS https://api.relayrouter.ai/v1/chat/completions \\\n  -H \"Authorization: Bearer $RELAYROUTER_API_KEY\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\n    \"model\": \"YOUR_MODEL_ID\",\n    \"messages\": [\n      {\"role\": \"user\", \"content\": \"Reply with one sentence.\"}\n    ]\n  }'\n```\n\nLook at the first lines of the response:\n\n`Content-Type: application/json` means you reached an API response, even if it contains an error.`Content-Type: text/html` means you probably reached a web page, redirect target, or proxy error page.`401` points toward the key or authorization header.`404` can mean the route or model ID is wrong.`429` calls for a check of rate limits, account status, and retry behavior.\nDo not paste a real key into a post or issue. Keep it in an environment variable and remove secrets from any logs you share.\n\nThis is where a URL fix can look successful but still leave the integration broken.\n\n| Client | Typical API route | Request format | \n|---|---|---|\n| OpenAI-compatible SDK | `/v1/chat/completions` | `model` plus`messages` in OpenAI Chat Completions format | \n| Claude Code | `/v1/messages` | Anthropic Messages format and headers | \n\nAnthropic’s [gateway compatibility guide](https://code.claude.com/docs/en/llm-gateway-protocol) lists `/v1/messages` as the relevant route for a gateway reached through `ANTHROPIC_BASE_URL`. It also describes headers and streaming behavior that the gateway must handle.\n\nSo the statement “just add `/v1` and Claude Code will work with any OpenAI-compatible API” is incomplete. The gateway needs an Anthropic-compatible route or a tested translation layer. Anthropic’s [gateway overview](https://code.claude.com/docs/en/llm-gateway) also notes that it does not support routing Claude Code to non-Claude models through a gateway.\n\nI use this sequence because each step removes one class of uncertainty:\n\n`/v1`.` Content-Type` before involving an SDK.\nFor an OpenAI-compatible Python client, the route looks like this:\n\n``` python\nimport os\nfrom openai import OpenAI\n\nclient = OpenAI(\n    api_key=os.environ[\"RELAYROUTER_API_KEY\"],\n    base_url=\"https://api.relayrouter.ai/v1\",\n)\n\nresponse = client.chat.completions.create(\n    model=os.environ[\"RELAYROUTER_MODEL\"],\n    messages=[{\"role\": \"user\", \"content\": \"Say hello in one sentence.\"}],\n)\n\nprint(response.choices[0].message.content)\n```\n\nFor Claude Code, follow Anthropic’s [gateway connection instructions](https://code.claude.com/docs/en/llm-gateway-connect) **after** confirming that your chosen gateway exposes the Anthropic Messages API. Its `ANTHROPIC_BASE_URL` is the gateway origin that Claude Code uses to reach `/v1/messages`; it is not interchangeable with an OpenAI SDK’s `base_url`.\n\nA base URL is only one part of the contract:\n\n```\nworking integration =\n  correct host\n  + correct path\n  + correct protocol\n  + valid credential\n  + available model\n```\n\nIf the response starts with HTML, inspect the route before changing application code. If the route returns JSON but Claude Code still fails, inspect protocol compatibility next.\n\nThe [RelayRouter quickstart repository](https://github.com/whale0yep-ai/relayrouter-api-quickstart) contains OpenAI-compatible examples and a troubleshooting guide. Use its current model and endpoint instructions alongside the service dashboard.\n\nIf you are debugging an API integration, compare the request URL, response status, and content type before sharing logs. You can discuss the setup in the [Discord community](https://discord.gg/MEdCarjE).\n\nDisclosure: This post includes an affiliate / referral link. If you register through it, I may earn a commission at no extra cost to you.\n\nIf you want to try the service, here is the [RelayRouter registration link](https://relayrouter.app/register?aff=xc9N).", "url": "https://wpnews.pro/news/relayrouter-api-troubleshooting-why-claude-code-gets-html-instead-of-json", "canonical_source": "https://dev.to/relayrouter/relayrouter-api-troubleshooting-why-claude-code-gets-html-instead-of-json-8cd", "published_at": "2026-12-03 07:38:56+00:00", "updated_at": "2026-12-03 07:47:16.348925+00:00", "lang": "en", "topics": ["ai-tools", "developer-tools", "ai-agents", "large-language-models"], "entities": ["RelayRouter", "Claude Code", "Anthropic", "OpenAI"], "also_reported_by": [], "takeaway": "When an SDK throws 'Unexpected token `<`', inspect the response's status code, Content-Type, and final URL first; text/html means you hit a web page or proxy error, and Claude Code needs an Anthropic Messages-compatible /v1/messages route, not just an OpenAI-compatible /v1/chat/completions endpoint.", "recommendation": {"advice": "Before debugging SDK code, curl the exact endpoint with -i and check status code and Content-Type; confirm the gateway exposes /v1/messages for Claude Code and /v1/chat/completions for OpenAI-compatible clients.", "playbook": "production-ops", "playbook_url": "https://wpnews.pro/playbooks/production-ops"}, "alternates": {"html": "https://wpnews.pro/news/relayrouter-api-troubleshooting-why-claude-code-gets-html-instead-of-json", "markdown": "https://wpnews.pro/news/relayrouter-api-troubleshooting-why-claude-code-gets-html-instead-of-json.md", "text": "https://wpnews.pro/news/relayrouter-api-troubleshooting-why-claude-code-gets-html-instead-of-json.txt", "jsonld": "https://wpnews.pro/news/relayrouter-api-troubleshooting-why-claude-code-gets-html-instead-of-json.jsonld"}}