Actions3
Direct API Call
AI-generatedOverview
This node enables direct interaction with the Claude Code AI models, allowing users to send prompts or instructions and receive generated responses. It supports two authentication methods: using stored API credentials or leveraging an existing CLI-based authentication. The node is designed for advanced use cases including direct API calls that bypass the CLI, as well as conversational interactions with multi-turn capabilities.
Common scenarios where this node is beneficial include:
- Generating code snippets or functions based on natural language prompts.
- Automating complex reasoning tasks by selecting from different Claude models.
- Integrating AI-assisted coding or content generation workflows within n8n.
- Using built-in tools during execution (e.g., file editing, web search) when not in direct API mode.
- Streaming responses for real-time output consumption.
Practical example:
- A user wants to generate a Python function to parse CSV files. They provide a prompt describing the task, select a capable Claude model, and receive the generated code snippet as output.
- Another user may want to perform a direct API call with specific temperature and token limits to get deterministic or creative completions.
Properties
| Name | Meaning |
|---|---|
| Authentication Method | How to authenticate with Claude Code: "Use Credentials" (via stored API key credential) or "Use CLI" (use existing CLI authentication). |
| Prompt | The instruction or query sent to Claude Code. Supports expressions to dynamically use data from previous nodes. |
| Model | Claude model to use. Options include: - Claude 3 Opus (complex reasoning) - Claude 3.5 Haiku (fast, simple tasks) - Claude 3.5 Sonnet (latest, most capable) - Legacy Opus (CLI) - Legacy Sonnet (CLI) |
| Max Tokens | Maximum number of tokens allowed in the response (only for Direct API Call operation). |
| Temperature | Controls randomness of the response (0 = deterministic, 1 = creative) (only for Direct API Call operation). |
| Max Turns | Maximum number of conversation turns allowed (back-and-forth exchanges) (hidden for Direct API Call). |
| Timeout | Maximum time in seconds to wait for completion before aborting. |
| Project Path | Directory path where Claude Code should run, allowing access to files and commands in that location (hidden for Direct API Call). |
| Output Format | Format of the output data: - Structured: object with messages, summary, result, and metrics - Messages: raw array of all exchanged messages - Text: only final result text |
| Allowed Tools | Built-in tools Claude Code is allowed to use during execution (hidden for Direct API Call). Options include Bash, Edit, Exit Plan Mode, Glob, Grep, LS, MultiEdit, Notebook Edit/Read, Read, Task, Todo Write, Web Fetch/Search, Write |
| Additional Options | Collection of optional settings: - System Prompt: extra context or instructions - Require Permissions: whether to require permission for tool use (hidden for Direct API Call) - Debug Mode: enable debug logging - Enable Streaming: enable streaming responses (for Direct API Call) |
Output
The node outputs JSON data structured according to the selected output format:
Structured: An object containing:
messages: Array of all exchanged messages (user, assistant, system).summary: Summary info such as message counts, tool usage, model used, duration, and cost metrics.result: The final textual result or error message.metrics: Performance and usage statistics (duration, token usage, cost).success: Boolean indicating if the operation succeeded.- Additional flags like
direct_apiorenhanceddepending on execution path.
Messages: Raw array of all messages exchanged during the session, including user inputs, assistant replies, and any tool uses.
Text: Only the final result text string returned by Claude Code.
If streaming is enabled (Direct API Call), the node can stream partial results as they arrive.
No binary data output is produced by this node.
Dependencies
Requires either:
- An API key credential for Claude Code's API service, configured in n8n credentials.
- Or an existing authenticated Claude Code CLI environment accessible to n8n.
Uses the official Claude Code SDK and API client libraries.
Network access to Claude Code API endpoints or local CLI environment.
Optional: configuration of timeout and project directory paths for CLI operations.
Troubleshooting
Prompt is required and cannot be empty: Ensure the prompt input is provided and not just whitespace.
Direct API call failed: Could indicate invalid API key, network issues, or API errors. Verify credentials and connectivity.
Operation timed out after X seconds: The request exceeded the configured timeout. Increase the timeout setting in Additional Options if needed.
AbortError: Usually caused by timeout aborting the request.
Debug Mode: Enable debug logging to see detailed console output for troubleshooting.
Streaming issues: If enabling streaming, ensure your workflow can handle streamed partial responses.
Authentication method mismatch: Make sure the selected authentication method matches your setup (credentials vs CLI).
Links and References
- Claude Code official documentation: https://claude.ai/docs/code
- Anthropic Claude API docs: https://docs.anthropic.com/api/
- n8n documentation on custom nodes and credentials: https://docs.n8n.io/
- Claude Code SDK repository: https://github.com/anthropic-ai/claude-code-sdk