{"slug": "claude-code-api-call-claude-code-from-your-own-code", "title": "Claude Code API: call Claude Code from your own code", "summary": "Anthropic's documentation now describes three ways to start Claude Code from a developer's own code: the claude -p (or --print) non-interactive CLI flag, the Agent SDK for Python and TypeScript, and an HTTP API trigger for cloud scheduled tasks, formerly called routines. The claude -p route exits with code 0 on success and a non-zero code on failure, and with --output-format json returns a total_cost_usd client-side cost estimate that Anthropic warns can differ from the actual bill; the docs recommend --bare for CI and scripts, which skips auto-discovery of hooks, skills, plugins, MCP servers and CLAUDE.md and requires an ANTHROPIC_API_KEY instead of a subscription login. The /fire endpoint at api.anthropic.com/v1/claude_code/routines/<trigger-id>/fire is available to claude.ai users only, ships under the experimental-cc-routine-2026-04-01 beta header while scheduled tasks are in research preview, and draws down subscription usage the same way interactive sessions do.", "body_md": "# Claude Code API: call Claude Code from your own code\n\nNovember 20, 2026 · 4 min read\n\nAnthropic’s docs describe three ways to start Claude Code from your own code: the claude -p command, the Agent SDK for Python and TypeScript, and the API trigger of a cloud scheduled task, formerly a routine. To call Claude models directly instead, the same docs point to the Client SDK for the Claude API.\n\nWhen you call Claude Code for the same job every day, there is a fourth option: have it write the job once as a Whenever workflow that runs on a webhook or a schedule, with no AI model call on a run unless you add an AI step.\n\n## Run Claude Code from a script with claude -p\n\nAnthropic’s docs call this running Claude Code in non-interactive mode: add the -p (or --print) flag to any claude command, with your prompt and the CLI options you need.\n\n```\nclaude -p \"What does the auth module do?\"\n```\n\nClaude Code exits with code 0 on success and a non-zero code when the run fails, so a script can branch on the exit status. With --output-format json, the response includes total_cost_usd, a client-side cost estimate that can differ from your actual bill.\n\n```\nclaude -p \"Summarize this project\" --output-format json | jq -r '.result'\n```\n\nFor CI and scripts, the docs recommend --bare: it skips auto-discovery of hooks, skills, plugins, MCP servers and CLAUDE.md, and it does not use your subscription login, so set ANTHROPIC_API_KEY with a key created in the Claude Console.\n\n## Embed it with the Agent SDK\n\nThe Agent SDK gives you the same tools, agent loop and context management that power Claude Code, as a Python or TypeScript library in a process you operate. From any other language, the docs say to run the CLI as a subprocess with the -p flag and --output-format json.\n\nA product you offer to other people authenticates with an API key: unless previously approved, Anthropic does not allow third-party developers to offer claude.ai login or rate limits for their products, including agents built on the Agent SDK.\n\n## Start a cloud routine from an HTTP request\n\nA cloud scheduled task, formerly a routine, can have an API trigger: a dedicated endpoint you POST to with the task’s bearer token, which starts a new session and returns its URL. An optional text field passes run-specific context, such as an alert body, alongside the saved prompt.\n\n```\ncurl -X POST https://api.anthropic.com/v1/claude_code/routines/<trigger-id>/fire \\\n  -H \"Authorization: Bearer <token>\" \\\n  -H \"anthropic-beta: experimental-cc-routine-2026-04-01\" \\\n  -H \"anthropic-version: 2023-06-01\" \\\n  -H \"Content-Type: application/json\" \\\n  -d '{\"text\": \"Sentry alert fired in prod.\"}'\n```\n\nThe /fire endpoint is available to claude.ai users only and is not part of the Claude Platform API surface. It ships under a beta header while scheduled tasks are in research preview, and scheduled tasks draw down subscription usage the same way interactive sessions do.\n\n## Each call starts Claude again\n\nAll three routes run Claude Code’s agent loop: claude -p and the Agent SDK in your process, and a routine’s API trigger in a new cloud session each time it fires. That is what you want while the task needs judgment, such as reading code and deciding what to change.\n\nIt is the wrong shape for a task whose steps are the same every time: a daily report, a sync between two tools, a check that posts to Slack. Each call asks the model to work out the same steps again.\n\n## Turn a repeated Claude Code task into a Whenever workflow\n\nAsk Claude Code to write the job once as a Whenever workflow. Connect it to the Whenever MCP server, or paste one line:\n\n```\nTurn this task into a Whenever workflow and deploy it. Follow www.whenever.dev/skill.md\n```\n\nIt writes the workflow and saves a draft; you complete Setup in the browser and publish. The published workflow starts on its own schedule, webhook or app event: a webhook your system calls where it used to run claude -p or the /fire endpoint, or a schedule where a cron entry started it.\n\n- **It runs the same way every time:** every run executes the workflow’s saved, numbered version, so the same input gives the same result on run 1 and run 1,000.\n- **One run, however many steps:** a run is one execution, start to finish, whatever it touches.\n- **No AI model call on every run:** a published workflow calls a model only in an AI step you added.\n- **It keeps running without a session:** nobody opens Claude Code or a chat for it to run.\n\nHobby is free with no credit card: 100 runs per month and one active workflow. Pro includes 10,000 runs per month.\n\n## Keep calling Claude Code when\n\n- Each run needs Claude to read the code and decide what to change\n- The result is a pull request, a review or a fix, not a fixed set of API calls\n- The task is still changing from one week to the next\n\n## A few useful answers\n\n### Is there a Claude Code API?\n\nAnthropic’s docs cover three programmatic routes: claude -p and the Agent SDK run Claude Code from your code, and a cloud routine’s API trigger starts one over HTTP. To call Claude models directly, the docs point to the Client SDK for the Claude API.\n\n### Does claude -p need an API key?\n\nIn bare mode, yes. Bare mode never reads OAuth credentials or the system keychain, so set ANTHROPIC_API_KEY with a key created in the Claude Console, or supply an apiKeyHelper. The docs recommend bare mode for scripted and SDK calls.\n\n### Can a webhook start Claude Code?\n\nYes, through a cloud routine’s API trigger, for claude.ai users. If the job behind the webhook is the same every time, a Whenever workflow can receive the webhook itself and run the steps without starting Claude.\n\n## Sources & further reading\n\nSources reviewed November 20, 2026.\n\n- [Run Claude Code programmatically - Claude Code Docs](https://code.claude.com/docs/en/headless)\n- [Agent SDK overview - Claude Code Docs](https://code.claude.com/docs/en/agent-sdk/overview)\n- [Automate work with scheduled tasks - Claude Code Docs](https://code.claude.com/docs/en/routines)\n\n## Choose your next step\n\n## Your first workflow is one sentence away.\n\nNo credit card required. One workflow is free. AI for building has weekly usage limits.", "url": "https://wpnews.pro/news/claude-code-api-call-claude-code-from-your-own-code", "canonical_source": "https://www.whenever.dev/blog/claude-code-api", "published_at": "2026-11-20 18:00:22+00:00", "updated_at": "2026-11-20 18:11:44.038560+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "large-language-models"], "entities": ["Anthropic", "Claude Code", "Agent SDK", "Claude API", "Whenever", "Claude Console", "claude.ai"], "also_reported_by": [], "takeaway": "Use claude -p with --bare and an ANTHROPIC_API_KEY for CI and scripts, since --bare skips auto-discovery of hooks, skills, plugins, MCP servers and CLAUDE.md and does not use your subscription login.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/claude-code-api-call-claude-code-from-your-own-code", "markdown": "https://wpnews.pro/news/claude-code-api-call-claude-code-from-your-own-code.md", "text": "https://wpnews.pro/news/claude-code-api-call-claude-code-from-your-own-code.txt", "jsonld": "https://wpnews.pro/news/claude-code-api-call-claude-code-from-your-own-code.jsonld"}}