Skip to main content

Your First Query

Let’s create a simple script that asks the AI to analyze a directory.

Step 1: Install the SDK

Step 2: Set Up Authentication

Export your API key:

Step 3: Create Your Script

Create a file called example.ts:
example.ts

Step 4: Run It

You should see the AI analyzing your directory and listing files!

Understanding the Output

The SDK emits different message types as the query progresses:

Message Types

Assistant Messages

Contain the AI’s responses:

System Messages

Provide session information:

Result Messages

Indicate query completion:

Adding Permission Control

By default, the SDK denies all tool executions. Let’s allow the AI to read files:

Using Permission Modes

Instead of writing a custom canUseTool handler, use built-in permission modes:

YOLO Mode (Auto-Approve Everything)

Auto-Edit Mode (Auto-Approve Edits)

Plan Mode (Block All Writes)

Streaming Partial Messages

Get real-time updates as the AI generates responses:

Aborting Queries

Cancel long-running queries:

Complete Example

Here’s a complete example that demonstrates multiple features:
complete-example.ts
Run it:

Next Steps

API Reference

Learn about all available options

Message Types

Understand different message types

Permission Modes

Master permission control

Examples

Explore more examples