import{claude}from'@instantlyeasy/claude-code-sdk-ts';// Create a file with automatic result extractionconstresult=awaitclaude().allowTools('Write').skipPermissions().query('Create a hello.txt file with "Hello from Claude!"').asResult();console.log('Task completed:',result);// Read and parse JSON dataconstconfig=awaitclaude().allowTools('Read').query('Read package.json and return its content').asJSON();console.log('Version:',config.version);
import{claude,ConsoleLogger,LogLevel}from'@instantlyeasy/claude-code-sdk-ts';// Full example with logging, event handlers, and response parsingconstlogger=newConsoleLogger(LogLevel.DEBUG);constanalysis=awaitclaude().withModel('opus').allowTools('Read','Grep','Glob').inDirectory(process.cwd()).withLogger(logger).onToolUse(tool=>console.log(`Using ${tool.name}...`)).query('Find all TODO comments in the codebase').asToolExecutions();// Get detailed tool execution resultsfor(constexecutionofanalysis){console.log(`${execution.tool}: ${execution.isError ? 'Failed' : 'Success'}`);}
Classic API (Original Syntax)
The original async generator API is still fully supported:
interfaceClaudeCodeOptions{// Model selectionmodel?: string;// Claude model to use (e.g., 'opus', 'sonnet')// Tool configurationallowedTools?: ToolName[];// Explicitly allowed toolsdeniedTools?: ToolName[];// Explicitly denied tools// Permission handlingpermissionMode?: PermissionMode;// 'default' | 'acceptEdits' | 'bypassPermissions'// Execution environmentcwd?: string;// Working directoryenv?: Record<string,string>;// Environment variables// MCP (Model Context Protocol) serversmcpServers?: MCPServer[];// MCP servers to connect// SDK optionstimeout?: number;// Timeout in millisecondsdebug?: boolean;// Enable debug logging (Note: may interfere with JSON parsing)// Deprecated options (not used by CLI transport)apiKey?: string;// Use `claude login` insteadbaseUrl?: string;// Not applicable for CLImaxTokens?: number;// Not configurable via CLItemperature?: number;// Not configurable via CLItools?: ToolName[];// Use allowedTools/deniedTools insteadcontext?: string[];// Not implemented}
The fluent API dramatically reduces boilerplate code. Here are some common migration patterns:
letfullText='';forawait(constmessageofquery('Generate a story')){if(message.type==='assistant'){for(constblockofmessage.content){if(block.type==='text'){fullText+=block.text;}}}}console.log(fullText);
constfullText=awaitclaude().query('Generate a story').asText();console.log(fullText);