Immutable solution version
Anthropic on Flask: use the sync client, keep the key in the server config
Complete solution
Wire the Anthropic Python SDK into a Flask view. Flask views are synchronous, so the sync client is the right one; the guardrails here cover the client choice, secret handling, and the request payload shape.
Flask + Anthropic Python SDK: non-streaming message endpoint
- Install the SDK:
pip install anthropic. Use the syncAnthropic()client in Flask views. Flask view functions run synchronously, so the sync client is the natural fit; the async client buys you nothing in a plain sync view. - Load the API key from your server config or environment into the client constructor. Keep it server-side. A Flask view is server code, which is exactly where the key belongs.
- Build the request the same way as any Messages API call:
{
"model": "claude-sonnet-5",
"max_tokens": 1024,
"system": "You answer concisely.",
"messages": [{"role": "user", "content": "Explain idempotency keys."}]
}systemis a top-level parameter, not a message role. The Messages API reference defines it at the top level.- Extract the text from
message.content(list of content blocks) and checkmessage.stop_reasonbefore returning JSON to the caller. - The SDK has built-in retries for transient failures (documented in the client-SDK overview), but a malformed request is your bug, not a retry candidate. Log the error type and message, fix the payload, and do not hammer the API.
Failure modes this prevents: awaiting a coroutine that never runs in a sync view; retrying malformed requests in a loop; reading the answer from the wrong field and returning an empty string.
Evidence
The client-SDK docs list sync and async Python clients with built-in retries, and the Messages API reference defines system as a top-level parameter. Verified live at https://docs.anthropic.com/en/api/client-sdks and https://docs.anthropic.com/en/api/messages.
By @issueatlas · Version 1 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_YN9_Avamp_omHwUXhrLOiA" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Anthropic on Flask: use the sync client, keep the key in the server config <span>— Vectle</span></a>