claude_code_sdk 1.1.0 copy "claude_code_sdk: ^1.1.0" to clipboard
claude_code_sdk: ^1.1.0 copied to clipboard

A Dart SDK for interacting with Claude Code, providing seamless integration with AI-powered coding assistance through the Claude Code CLI.

example/claude_code_sdk_example.dart

import 'dart:io';
import 'package:claude_code_sdk/claude_code_sdk.dart';

/// Main example demonstrating the Claude Code SDK usage
void main() async {
  // Get API key from environment variable
  final apiKey = Platform.environment['ANTHROPIC_API_KEY'];
  
  if (apiKey == null || apiKey.isEmpty) {
    print('Please set ANTHROPIC_API_KEY environment variable');
    print('export ANTHROPIC_API_KEY="your-api-key-here"');
    exit(1);
  }

  // Initialize the SDK
  final claudeSDK = Claude(apiKey);

  // Check if Claude Code CLI is installed
  final isInstalled = await claudeSDK.isClaudeCodeSDKInstalled();
  if (!isInstalled) {
    print('Claude Code CLI is not installed.');
    print('Installing...');
    await claudeSDK.installClaudeCodeSDK();
  }

  // Create a chat session
  final chat = claudeSDK.createNewChat(
    options: ClaudeChatOptions(
      systemPrompt: 'You are a helpful assistant',
      maxTurns: 3,
    ),
  );

  try {
    // Send a simple message
    print('Sending message to Claude...');
    final response = await chat.sendMessage([
      ClaudeSdkContent.text('Hello Claude! Please introduce yourself briefly.'),
    ]);
    
    print('Response: $response');

    // Example with schema
    final schema = SchemaObject(
      properties: {
        'greeting': SchemaProperty.string(
          description: 'A greeting message',
        ),
        'name': SchemaProperty.string(
          description: 'Your name',
        ),
        'capabilities': SchemaProperty.array(
          items: SchemaProperty.string(),
          description: 'List of your main capabilities',
        ),
      },
      required: ['greeting', 'name'],
    );

    print('\nSending message with schema...');
    final schemaResult = await chat.sendMessageWithSchema(
      messages: [
        ClaudeSdkContent.text('Please introduce yourself'),
      ],
      schema: schema,
    );

    print('Structured response:');
    print('  Greeting: ${schemaResult.data['greeting']}');
    print('  Name: ${schemaResult.data['name']}');
    print('  Capabilities: ${schemaResult.data['capabilities']}');
    
  } catch (e) {
    print('Error: $e');
  } finally {
    // Always dispose of resources
    await chat.dispose();
    await claudeSDK.dispose();
  }
}
1
likes
150
points
186
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart SDK for interacting with Claude Code, providing seamless integration with AI-powered coding assistance through the Claude Code CLI.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

path, uuid

More

Packages that depend on claude_code_sdk