|
| 1 | +## Using Claude Agent SDK with Trigger.dev |
| 2 | + |
| 3 | +This is a secure example of how to use the [Claude Agent SDK](https://docs.claude.com/en/docs/agent-sdk/overview) with [Trigger.dev](https://trigger.dev). |
| 4 | + |
| 5 | +**Two example implementations:** |
| 6 | + |
| 7 | +- `code.ts` - Basic safe code generation (recommended starting point) |
| 8 | +- `code-advanced.ts` - Advanced with bash execution and approval system |
| 9 | + |
| 10 | +### Setup |
| 11 | + |
| 12 | +Create a new project in Trigger.dev and copy the project ref. |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install |
| 16 | +``` |
| 17 | + |
| 18 | +Copy the `.env.example` file to `.env` and fill in the values: |
| 19 | + |
| 20 | +```bash |
| 21 | +cp .env.example .env |
| 22 | +``` |
| 23 | + |
| 24 | +```bash |
| 25 | +TRIGGER_PROJECT_REF="<your trigger.dev project ref here, starts with proj_>" |
| 26 | +ANTHROPIC_API_KEY="sk-ant-api03-1234" |
| 27 | +``` |
| 28 | + |
| 29 | +The Claude Agent SDK will automatically use the `ANTHROPIC_API_KEY` environment variable for authentication. Model configuration and other settings are configured directly in the task code at `src/trigger/code.ts`. |
| 30 | + |
| 31 | +Authenticate the `trigger.dev` CLI with your Trigger.dev account: |
| 32 | + |
| 33 | +```bash |
| 34 | +npm run trigger:login |
| 35 | +``` |
| 36 | + |
| 37 | +### Run |
| 38 | + |
| 39 | +Run the dev command to register your tasks and test/run them locally. |
| 40 | + |
| 41 | +```bash |
| 42 | +npm run dev |
| 43 | +``` |
| 44 | + |
| 45 | +### View code |
| 46 | + |
| 47 | +**Basic example** (`src/trigger/code.ts`): |
| 48 | + |
| 49 | +- Safe code generation without bash execution |
| 50 | +- Isolated workspace with automatic cleanup |
| 51 | +- Permission mode: `acceptEdits` (auto-approves file operations) |
| 52 | +- Good for: generating code, analyzing files, data transformation |
| 53 | + |
| 54 | +**Advanced example** (`src/trigger/code-advanced.ts`): |
| 55 | + |
| 56 | +- Full development workflow with bash execution capability |
| 57 | +- Isolated workspace with automatic cleanup |
| 58 | +- Permission mode: `acceptEdits` (auto-approves file edits) |
| 59 | +- Bash tool enabled in allowedTools list |
| 60 | +- Good for: npm install, running tests, build commands |
| 61 | +- **Warning:** Only use with trusted users - enables command execution |
| 62 | + |
| 63 | +### Test |
| 64 | + |
| 65 | +You can trigger the `claude-code` task by sending a POST request: |
| 66 | + |
| 67 | +```bash |
| 68 | +curl -X POST "https://api.trigger.dev/api/v1/tasks/claude-code/trigger" \ |
| 69 | + -H "Content-Type: application/json" \ |
| 70 | + -H "Authorization: Bearer <your trigger.dev DEVELOPMENT API key here, starts with tr_dev_>" \ |
| 71 | + -d '{ |
| 72 | + "payload": { |
| 73 | + "prompt": "Write a simple hello world program in JavaScript.", |
| 74 | + "maxTurns": 3, |
| 75 | + "maxIterations": 10 |
| 76 | + }, |
| 77 | + }' |
| 78 | +``` |
| 79 | + |
| 80 | +Or you could visit the Trigger.dev Dashboard and use the "Test" page to trigger the task |
| 81 | + |
| 82 | +<img width="3680" height="2382" alt="CleanShot 2025-07-15 at 22 16 19@2x" src="https://github.com/user-attachments/assets/fe06c1dc-c49c-4cfb-89f7-bd8f69a77e90" /> |
0 commit comments