@icarusmx/creta 1.5.12 → 1.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/creta.js +31 -1
- package/lib/data/command-help/aws-ec2.js +34 -0
- package/lib/data/command-help/grep.js +76 -0
- package/lib/data/command-help/index.js +11 -1
- package/lib/data/command-help/lz.js +57 -0
- package/lib/executors/CommandHelpExecutor.js +6 -1
- package/lib/exercises/.claude/settings.local.json +12 -0
- package/lib/exercises/01-developing-muscle-for-nvim.md +528 -0
- package/lib/exercises/{iterm2-pane-navigation.md → 02-iterm2-pane-navigation.md} +1 -1
- package/lib/exercises/05-svelte-first-steps.md +1340 -0
- package/lib/exercises/{curl-and-pipes.md → 06-curl-and-pipes.md} +187 -72
- package/lib/exercises/07-claude-api-first-steps.md +855 -0
- package/lib/exercises/08-playwright-svelte-guide.md +1384 -0
- package/lib/exercises/09-docker-first-steps.md +1475 -0
- package/lib/exercises/{railway-deployment.md → 10-railway-deployment.md} +1 -0
- package/lib/exercises/{aws-billing-detective.md → 11-aws-billing-detective.md} +215 -35
- package/lib/exercises/12-install-skills.md +755 -0
- package/lib/exercises/13-shell-aliases.md +134 -0
- package/lib/exercises/README.md +187 -0
- package/lib/exercises/utils/booklet-2up.js +133 -0
- package/lib/exercises/utils/booklet-manual-duplex.js +159 -0
- package/lib/exercises/utils/booklet-simple.js +136 -0
- package/lib/exercises/utils/create-booklet.js +116 -0
- package/lib/scripts/aws-ec2-all.sh +58 -0
- package/package.json +3 -2
- /package/lib/exercises/{git-stash-workflow.md → 03-git-stash-workflow.md} +0 -0
- /package/lib/exercises/{array-object-manipulation.md → 04-array-object-manipulation.md} +0 -0
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
# Claude API First Steps - AI from the Terminal
|
|
2
|
+
|
|
3
|
+
<!-- vim: set foldmethod=marker foldlevel=0: -->
|
|
4
|
+
|
|
5
|
+
## 📖 LazyVim Reading Guide {{{
|
|
6
|
+
|
|
7
|
+
**Start with:** `zM` (close all folds) → Navigate with `za` (toggle fold under cursor)
|
|
8
|
+
|
|
9
|
+
This document uses fold markers `{{{` and `}}}` for organized reading.
|
|
10
|
+
|
|
11
|
+
}}}
|
|
12
|
+
|
|
13
|
+
## 🚨 Problem: Need AI Assistance Without Leaving Terminal {{{
|
|
14
|
+
|
|
15
|
+
You're deep in your workflow and need to:
|
|
16
|
+
- Ask Claude a quick question
|
|
17
|
+
- Generate code snippets
|
|
18
|
+
- Debug error messages
|
|
19
|
+
- Process text/data with AI
|
|
20
|
+
|
|
21
|
+
**Current workflow:**
|
|
22
|
+
1. Open browser
|
|
23
|
+
2. Go to claude.ai
|
|
24
|
+
3. Copy/paste question
|
|
25
|
+
4. Wait for response
|
|
26
|
+
5. Copy back to terminal
|
|
27
|
+
6. Switch back to work
|
|
28
|
+
|
|
29
|
+
**Time wasted:** 2-3 minutes per query
|
|
30
|
+
**Context lost:** Every tab switch breaks flow
|
|
31
|
+
|
|
32
|
+
There's a **better way** → Claude API from terminal
|
|
33
|
+
|
|
34
|
+
}}}
|
|
35
|
+
|
|
36
|
+
## ✅ Solution: curl + Claude API + Environment Variables {{{
|
|
37
|
+
|
|
38
|
+
Call Claude API directly from your terminal using curl with securely stored API keys.
|
|
39
|
+
|
|
40
|
+
**Core pattern:**
|
|
41
|
+
```bash
|
|
42
|
+
export ANTHROPIC_API_KEY="your-key-here"
|
|
43
|
+
curl https://api.anthropic.com/v1/messages \
|
|
44
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
45
|
+
-H "anthropic-version: 2023-06-01" \
|
|
46
|
+
-H "content-type: application/json" \
|
|
47
|
+
-d '{
|
|
48
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
49
|
+
"max_tokens": 1024,
|
|
50
|
+
"messages": [{"role": "user", "content": "Hello, Claude"}]
|
|
51
|
+
}'
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
**Benefits:**
|
|
55
|
+
- ⚡ **Instant** - No browser/UI switching
|
|
56
|
+
- 🔐 **Secure** - API keys in environment, not command history
|
|
57
|
+
- 🔁 **Scriptable** - Automate repetitive AI tasks
|
|
58
|
+
- 🎯 **Focused** - Stay in terminal flow state
|
|
59
|
+
|
|
60
|
+
}}}
|
|
61
|
+
|
|
62
|
+
## 🔑 Getting Your API Key {{{
|
|
63
|
+
|
|
64
|
+
### Step 1: Create Anthropic Account {{{
|
|
65
|
+
|
|
66
|
+
1. Go to https://console.anthropic.com
|
|
67
|
+
2. Sign up or log in
|
|
68
|
+
3. Navigate to "API Keys" section
|
|
69
|
+
|
|
70
|
+
}}}
|
|
71
|
+
|
|
72
|
+
### Step 2: Generate API Key {{{
|
|
73
|
+
|
|
74
|
+
1. Click "Create Key"
|
|
75
|
+
2. Name it (e.g., "Terminal Development")
|
|
76
|
+
3. Copy the key (starts with `sk-ant-api03-...`)
|
|
77
|
+
4. **IMPORTANT:** Save it immediately - you can't see it again
|
|
78
|
+
|
|
79
|
+
}}}
|
|
80
|
+
|
|
81
|
+
### Step 3: Store Securely {{{
|
|
82
|
+
|
|
83
|
+
**❌ NEVER do this:**
|
|
84
|
+
```bash
|
|
85
|
+
curl https://api.anthropic.com/v1/messages \
|
|
86
|
+
-H "x-api-key: sk-ant-api03-your-actual-key-here" \
|
|
87
|
+
-d '...'
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Why dangerous:**
|
|
91
|
+
- Saved in shell history forever
|
|
92
|
+
- Visible in `ps aux` output
|
|
93
|
+
- Accidentally committed to git
|
|
94
|
+
- Leaked in screenshots
|
|
95
|
+
|
|
96
|
+
**✅ Do this instead:** (covered in next section)
|
|
97
|
+
|
|
98
|
+
}}}
|
|
99
|
+
|
|
100
|
+
}}}
|
|
101
|
+
|
|
102
|
+
## 🔐 Secure API Key Storage {{{
|
|
103
|
+
|
|
104
|
+
### Progressive Security Levels
|
|
105
|
+
|
|
106
|
+
#### 🚨 Level 0: Hardcoded (NEVER) {{{
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
# ❌ DANGER: API key in history forever
|
|
110
|
+
curl https://api.anthropic.com/v1/messages \
|
|
111
|
+
-H "x-api-key: sk-ant-api03-ABC123..." \
|
|
112
|
+
-d '{"model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [{"role": "user", "content": "hello"}]}'
|
|
113
|
+
|
|
114
|
+
# Verify the problem
|
|
115
|
+
history | tail -n 1
|
|
116
|
+
# Shows your entire API key!
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Never do this.** Ever.
|
|
120
|
+
|
|
121
|
+
}}}
|
|
122
|
+
|
|
123
|
+
#### ✅ Level 1: Environment Variable (Quick & Safe) {{{
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
# Store in variable for current session
|
|
127
|
+
export ANTHROPIC_API_KEY="sk-ant-api03-ABC123..."
|
|
128
|
+
|
|
129
|
+
# Use variable (history only shows $ANTHROPIC_API_KEY)
|
|
130
|
+
curl https://api.anthropic.com/v1/messages \
|
|
131
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
132
|
+
-H "anthropic-version: 2023-06-01" \
|
|
133
|
+
-H "content-type: application/json" \
|
|
134
|
+
-d '{
|
|
135
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
136
|
+
"max_tokens": 1024,
|
|
137
|
+
"messages": [{"role": "user", "content": "Hello from terminal!"}]
|
|
138
|
+
}'
|
|
139
|
+
|
|
140
|
+
# Check history - no key visible!
|
|
141
|
+
history | tail -n 1
|
|
142
|
+
# Shows: curl ... -H "x-api-key: $ANTHROPIC_API_KEY" ...
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**Good for:** Testing, temporary work
|
|
146
|
+
**Limitation:** Lost when terminal closes
|
|
147
|
+
|
|
148
|
+
}}}
|
|
149
|
+
|
|
150
|
+
#### ✅ Level 2: Shell RC File (Persistent) {{{
|
|
151
|
+
|
|
152
|
+
**For zsh (macOS default):**
|
|
153
|
+
```bash
|
|
154
|
+
# Add to ~/.zshrc
|
|
155
|
+
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-ABC123..."' >> ~/.zshrc
|
|
156
|
+
|
|
157
|
+
# Reload shell config
|
|
158
|
+
source ~/.zshrc
|
|
159
|
+
|
|
160
|
+
# Test - key now available in all new terminals
|
|
161
|
+
echo $ANTHROPIC_API_KEY | head -c 20
|
|
162
|
+
# sk-ant-api03-ABC123...
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**For bash:**
|
|
166
|
+
```bash
|
|
167
|
+
# Add to ~/.bashrc (Linux) or ~/.bash_profile (macOS)
|
|
168
|
+
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-ABC123..."' >> ~/.bashrc
|
|
169
|
+
source ~/.bashrc
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Good for:** Daily development work
|
|
173
|
+
**Protection:** Add to .gitignore if you track dotfiles
|
|
174
|
+
|
|
175
|
+
}}}
|
|
176
|
+
|
|
177
|
+
#### ✅ Level 3: .env File (Project-Specific) {{{
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
# Create .env file in project root
|
|
181
|
+
cat > .env <<'EOF'
|
|
182
|
+
ANTHROPIC_API_KEY=sk-ant-api03-ABC123...
|
|
183
|
+
EOF
|
|
184
|
+
|
|
185
|
+
# CRITICAL: Protect and ignore
|
|
186
|
+
chmod 600 .env
|
|
187
|
+
echo '.env' >> .gitignore
|
|
188
|
+
|
|
189
|
+
# Load environment in script
|
|
190
|
+
source .env
|
|
191
|
+
|
|
192
|
+
# Or inline for one command
|
|
193
|
+
(source .env && curl https://api.anthropic.com/v1/messages \
|
|
194
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
195
|
+
-H "anthropic-version: 2023-06-01" \
|
|
196
|
+
-H "content-type: application/json" \
|
|
197
|
+
-d '{"model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}')
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
**Good for:** Shared projects, multiple API keys
|
|
201
|
+
**Best practice:** Never commit .env to git
|
|
202
|
+
|
|
203
|
+
}}}
|
|
204
|
+
|
|
205
|
+
#### ✅ Level 4: Secure File Read (No Environment) {{{
|
|
206
|
+
|
|
207
|
+
```bash
|
|
208
|
+
# Store key in protected file
|
|
209
|
+
echo 'sk-ant-api03-ABC123...' > ~/.anthropic-api-key
|
|
210
|
+
chmod 600 ~/.anthropic-api-key # Only you can read
|
|
211
|
+
|
|
212
|
+
# Read directly in subshell (never enters environment)
|
|
213
|
+
curl https://api.anthropic.com/v1/messages \
|
|
214
|
+
-H "x-api-key: $(cat ~/.anthropic-api-key)" \
|
|
215
|
+
-H "anthropic-version: 2023-06-01" \
|
|
216
|
+
-H "content-type: application/json" \
|
|
217
|
+
-d '{
|
|
218
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
219
|
+
"max_tokens": 1024,
|
|
220
|
+
"messages": [{"role": "user", "content": "Hello"}]
|
|
221
|
+
}'
|
|
222
|
+
|
|
223
|
+
# Verify: key never in environment
|
|
224
|
+
env | grep -i anthropic
|
|
225
|
+
# (empty)
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
**Good for:** Maximum security, sensitive environments
|
|
229
|
+
**Advantage:** Key never exposed in environment variables
|
|
230
|
+
|
|
231
|
+
}}}
|
|
232
|
+
|
|
233
|
+
#### 🔒 Level 5: macOS Keychain (Production) {{{
|
|
234
|
+
|
|
235
|
+
```bash
|
|
236
|
+
# Store in macOS Keychain
|
|
237
|
+
security add-generic-password \
|
|
238
|
+
-a "$USER" \
|
|
239
|
+
-s anthropic-api-key \
|
|
240
|
+
-w "sk-ant-api03-ABC123..."
|
|
241
|
+
|
|
242
|
+
# Retrieve and use (will prompt for password first time)
|
|
243
|
+
ANTHROPIC_API_KEY=$(security find-generic-password \
|
|
244
|
+
-a "$USER" \
|
|
245
|
+
-s anthropic-api-key \
|
|
246
|
+
-w)
|
|
247
|
+
|
|
248
|
+
curl https://api.anthropic.com/v1/messages \
|
|
249
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
250
|
+
-H "anthropic-version: 2023-06-01" \
|
|
251
|
+
-H "content-type: application/json" \
|
|
252
|
+
-d '{"model": "claude-3-5-sonnet-20241022", "max_tokens": 1024, "messages": [{"role": "user", "content": "Hello"}]}'
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
**Good for:** Professional development, shared machines
|
|
256
|
+
**Best practice:** OS-level encryption + biometric unlock
|
|
257
|
+
|
|
258
|
+
}}}
|
|
259
|
+
|
|
260
|
+
}}}
|
|
261
|
+
|
|
262
|
+
## 🎯 Basic Claude API Usage {{{
|
|
263
|
+
|
|
264
|
+
### Minimal Example {{{
|
|
265
|
+
|
|
266
|
+
**Simplest possible call:**
|
|
267
|
+
```bash
|
|
268
|
+
curl https://api.anthropic.com/v1/messages \
|
|
269
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
270
|
+
-H "anthropic-version: 2023-06-01" \
|
|
271
|
+
-H "content-type: application/json" \
|
|
272
|
+
-d '{
|
|
273
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
274
|
+
"max_tokens": 1024,
|
|
275
|
+
"messages": [
|
|
276
|
+
{"role": "user", "content": "Say hello in Spanish"}
|
|
277
|
+
]
|
|
278
|
+
}'
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
**Response structure:**
|
|
282
|
+
```json
|
|
283
|
+
{
|
|
284
|
+
"id": "msg_01ABC123...",
|
|
285
|
+
"type": "message",
|
|
286
|
+
"role": "assistant",
|
|
287
|
+
"content": [
|
|
288
|
+
{
|
|
289
|
+
"type": "text",
|
|
290
|
+
"text": "¡Hola! ¿Cómo estás?"
|
|
291
|
+
}
|
|
292
|
+
],
|
|
293
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
294
|
+
"stop_reason": "end_turn",
|
|
295
|
+
"usage": {
|
|
296
|
+
"input_tokens": 12,
|
|
297
|
+
"output_tokens": 8
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
}}}
|
|
303
|
+
|
|
304
|
+
### Extract Just the Response {{{
|
|
305
|
+
|
|
306
|
+
**Problem:** Full JSON is verbose, you just want Claude's text
|
|
307
|
+
|
|
308
|
+
**Solution:** Pipe to grep + sed
|
|
309
|
+
```bash
|
|
310
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
311
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
312
|
+
-H "anthropic-version: 2023-06-01" \
|
|
313
|
+
-H "content-type: application/json" \
|
|
314
|
+
-d '{
|
|
315
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
316
|
+
"max_tokens": 1024,
|
|
317
|
+
"messages": [{"role": "user", "content": "Say hello in Spanish"}]
|
|
318
|
+
}' | grep -o '"text":"[^"]*"' | sed 's/"text":"\(.*\)"/\1/'
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
**Output:**
|
|
322
|
+
```
|
|
323
|
+
¡Hola! ¿Cómo estás?
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
**With jq (cleaner):**
|
|
327
|
+
```bash
|
|
328
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
329
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
330
|
+
-H "anthropic-version: 2023-06-01" \
|
|
331
|
+
-H "content-type: application/json" \
|
|
332
|
+
-d '{
|
|
333
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
334
|
+
"max_tokens": 1024,
|
|
335
|
+
"messages": [{"role": "user", "content": "Say hello"}]
|
|
336
|
+
}' | jq -r '.content[0].text'
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
}}}
|
|
340
|
+
|
|
341
|
+
### Model Selection {{{
|
|
342
|
+
|
|
343
|
+
**Available models:**
|
|
344
|
+
```bash
|
|
345
|
+
# Claude 3.5 Sonnet (Best balance - RECOMMENDED)
|
|
346
|
+
"model": "claude-3-5-sonnet-20241022"
|
|
347
|
+
|
|
348
|
+
# Claude 3 Opus (Most capable, slower/expensive)
|
|
349
|
+
"model": "claude-3-opus-20240229"
|
|
350
|
+
|
|
351
|
+
# Claude 3 Haiku (Fastest, cheapest)
|
|
352
|
+
"model": "claude-3-haiku-20240307"
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
**Choosing:**
|
|
356
|
+
- **Daily coding:** Sonnet (great quality, fast)
|
|
357
|
+
- **Complex reasoning:** Opus (maximum intelligence)
|
|
358
|
+
- **Quick questions:** Haiku (instant responses)
|
|
359
|
+
|
|
360
|
+
}}}
|
|
361
|
+
|
|
362
|
+
### Token Limits {{{
|
|
363
|
+
|
|
364
|
+
**max_tokens controls response length:**
|
|
365
|
+
```bash
|
|
366
|
+
# Short answers (1-2 paragraphs)
|
|
367
|
+
"max_tokens": 256
|
|
368
|
+
|
|
369
|
+
# Medium responses (explanations)
|
|
370
|
+
"max_tokens": 1024
|
|
371
|
+
|
|
372
|
+
# Long responses (essays, code)
|
|
373
|
+
"max_tokens": 4096
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
**Cost note:** You pay for input + output tokens, not just max_tokens
|
|
377
|
+
|
|
378
|
+
}}}
|
|
379
|
+
|
|
380
|
+
}}}
|
|
381
|
+
|
|
382
|
+
## 🔧 Practical Use Cases {{{
|
|
383
|
+
|
|
384
|
+
### Use Case 1: Quick Code Explanation {{{
|
|
385
|
+
|
|
386
|
+
```bash
|
|
387
|
+
# Ask Claude to explain a command
|
|
388
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
389
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
390
|
+
-H "anthropic-version: 2023-06-01" \
|
|
391
|
+
-H "content-type: application/json" \
|
|
392
|
+
-d '{
|
|
393
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
394
|
+
"max_tokens": 512,
|
|
395
|
+
"messages": [
|
|
396
|
+
{"role": "user", "content": "Explain this command: grep -r \"TODO\" . | wc -l"}
|
|
397
|
+
]
|
|
398
|
+
}' | jq -r '.content[0].text'
|
|
399
|
+
```
|
|
400
|
+
|
|
401
|
+
**Output:**
|
|
402
|
+
```
|
|
403
|
+
This command searches recursively through all files in the current directory
|
|
404
|
+
for lines containing "TODO" and counts how many matches are found.
|
|
405
|
+
|
|
406
|
+
Breakdown:
|
|
407
|
+
- grep -r "TODO" . → searches recursively for "TODO"
|
|
408
|
+
- | → pipes results to next command
|
|
409
|
+
- wc -l → counts lines (number of matches)
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
}}}
|
|
413
|
+
|
|
414
|
+
### Use Case 2: Debug Error Messages {{{
|
|
415
|
+
|
|
416
|
+
```bash
|
|
417
|
+
# Capture error output and ask Claude
|
|
418
|
+
ERROR_MSG=$(npm run build 2>&1 | tail -n 20)
|
|
419
|
+
|
|
420
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
421
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
422
|
+
-H "anthropic-version: 2023-06-01" \
|
|
423
|
+
-H "content-type: application/json" \
|
|
424
|
+
-d "{
|
|
425
|
+
\"model\": \"claude-3-5-sonnet-20241022\",
|
|
426
|
+
\"max_tokens\": 1024,
|
|
427
|
+
\"messages\": [
|
|
428
|
+
{\"role\": \"user\", \"content\": \"I got this error. What's wrong and how do I fix it?\n\n$ERROR_MSG\"}
|
|
429
|
+
]
|
|
430
|
+
}" | jq -r '.content[0].text'
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
}}}
|
|
434
|
+
|
|
435
|
+
### Use Case 3: Generate Code Snippets {{{
|
|
436
|
+
|
|
437
|
+
```bash
|
|
438
|
+
# Generate a function
|
|
439
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
440
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
441
|
+
-H "anthropic-version: 2023-06-01" \
|
|
442
|
+
-H "content-type: application/json" \
|
|
443
|
+
-d '{
|
|
444
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
445
|
+
"max_tokens": 512,
|
|
446
|
+
"messages": [
|
|
447
|
+
{"role": "user", "content": "Write a bash function that checks if a port is in use"}
|
|
448
|
+
]
|
|
449
|
+
}' | jq -r '.content[0].text'
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
**Save directly to file:**
|
|
453
|
+
```bash
|
|
454
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
455
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
456
|
+
-H "anthropic-version: 2023-06-01" \
|
|
457
|
+
-H "content-type: application/json" \
|
|
458
|
+
-d '{
|
|
459
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
460
|
+
"max_tokens": 512,
|
|
461
|
+
"messages": [
|
|
462
|
+
{"role": "user", "content": "Write a bash function that checks if a port is in use. Only output the code, no explanation."}
|
|
463
|
+
]
|
|
464
|
+
}' | jq -r '.content[0].text' > port-check.sh
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
}}}
|
|
468
|
+
|
|
469
|
+
### Use Case 4: Process Files with AI {{{
|
|
470
|
+
|
|
471
|
+
```bash
|
|
472
|
+
# Send file contents to Claude
|
|
473
|
+
FILE_CONTENT=$(cat README.md)
|
|
474
|
+
|
|
475
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
476
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
477
|
+
-H "anthropic-version: 2023-06-01" \
|
|
478
|
+
-H "content-type: application/json" \
|
|
479
|
+
-d "{
|
|
480
|
+
\"model\": \"claude-3-5-sonnet-20241022\",
|
|
481
|
+
\"max_tokens\": 1024,
|
|
482
|
+
\"messages\": [
|
|
483
|
+
{\"role\": \"user\", \"content\": \"Summarize this README in 3 bullet points:\n\n$FILE_CONTENT\"}
|
|
484
|
+
]
|
|
485
|
+
}" | jq -r '.content[0].text'
|
|
486
|
+
```
|
|
487
|
+
|
|
488
|
+
}}}
|
|
489
|
+
|
|
490
|
+
### Use Case 5: Interactive Terminal Helper {{{
|
|
491
|
+
|
|
492
|
+
```bash
|
|
493
|
+
# Create a simple ask() function
|
|
494
|
+
ask() {
|
|
495
|
+
QUESTION="$1"
|
|
496
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
497
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
498
|
+
-H "anthropic-version: 2023-06-01" \
|
|
499
|
+
-H "content-type: application/json" \
|
|
500
|
+
-d "{
|
|
501
|
+
\"model\": \"claude-3-5-sonnet-20241022\",
|
|
502
|
+
\"max_tokens\": 512,
|
|
503
|
+
\"messages\": [
|
|
504
|
+
{\"role\": \"user\", \"content\": \"$QUESTION\"}
|
|
505
|
+
]
|
|
506
|
+
}" | jq -r '.content[0].text'
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
# Usage
|
|
510
|
+
ask "How do I exit vim?"
|
|
511
|
+
ask "What does chmod 755 mean?"
|
|
512
|
+
ask "Git command to undo last commit but keep changes?"
|
|
513
|
+
```
|
|
514
|
+
|
|
515
|
+
**Add to ~/.zshrc for permanent access:**
|
|
516
|
+
```bash
|
|
517
|
+
echo '
|
|
518
|
+
# Claude Terminal Helper
|
|
519
|
+
ask() {
|
|
520
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
521
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
522
|
+
-H "anthropic-version: 2023-06-01" \
|
|
523
|
+
-H "content-type: application/json" \
|
|
524
|
+
-d "{
|
|
525
|
+
\"model\": \"claude-3-5-sonnet-20241022\",
|
|
526
|
+
\"max_tokens\": 512,
|
|
527
|
+
\"messages\": [
|
|
528
|
+
{\"role\": \"user\", \"content\": \"$1\"}
|
|
529
|
+
]
|
|
530
|
+
}" | jq -r ".content[0].text"
|
|
531
|
+
}
|
|
532
|
+
' >> ~/.zshrc
|
|
533
|
+
|
|
534
|
+
source ~/.zshrc
|
|
535
|
+
```
|
|
536
|
+
|
|
537
|
+
}}}
|
|
538
|
+
|
|
539
|
+
}}}
|
|
540
|
+
|
|
541
|
+
## 🧪 Practice Exercises {{{
|
|
542
|
+
|
|
543
|
+
### Exercise 1: Basic API Call {{{
|
|
544
|
+
|
|
545
|
+
**Goal:** Get Claude to respond with a greeting
|
|
546
|
+
|
|
547
|
+
**Your command:**
|
|
548
|
+
```bash
|
|
549
|
+
export ANTHROPIC_API_KEY="your-key-here"
|
|
550
|
+
|
|
551
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
552
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
553
|
+
-H "anthropic-version: 2023-06-01" \
|
|
554
|
+
-H "content-type: application/json" \
|
|
555
|
+
-d '{
|
|
556
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
557
|
+
"max_tokens": 256,
|
|
558
|
+
"messages": [{"role": "user", "content": "Say hello"}]
|
|
559
|
+
}'
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
**Expected:** JSON response with Claude's greeting
|
|
563
|
+
|
|
564
|
+
}}}
|
|
565
|
+
|
|
566
|
+
### Exercise 2: Extract Just Text {{{
|
|
567
|
+
|
|
568
|
+
**Goal:** Get only Claude's response text, no JSON
|
|
569
|
+
|
|
570
|
+
**Your command:**
|
|
571
|
+
```bash
|
|
572
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
573
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
574
|
+
-H "anthropic-version: 2023-06-01" \
|
|
575
|
+
-H "content-type: application/json" \
|
|
576
|
+
-d '{
|
|
577
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
578
|
+
"max_tokens": 256,
|
|
579
|
+
"messages": [{"role": "user", "content": "Say hello in French"}]
|
|
580
|
+
}' | jq -r '.content[0].text'
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
**Expected:** Just the text "Bonjour!" (or similar)
|
|
584
|
+
|
|
585
|
+
}}}
|
|
586
|
+
|
|
587
|
+
### Exercise 3: Ask About a Command {{{
|
|
588
|
+
|
|
589
|
+
**Goal:** Use Claude to explain a terminal command
|
|
590
|
+
|
|
591
|
+
**Your command:**
|
|
592
|
+
```bash
|
|
593
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
594
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
595
|
+
-H "anthropic-version: 2023-06-01" \
|
|
596
|
+
-H "content-type: application/json" \
|
|
597
|
+
-d '{
|
|
598
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
599
|
+
"max_tokens": 512,
|
|
600
|
+
"messages": [
|
|
601
|
+
{"role": "user", "content": "Explain: ps aux | grep node"}
|
|
602
|
+
]
|
|
603
|
+
}' | jq -r '.content[0].text'
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
**Expected:** Claude explains the command breakdown
|
|
607
|
+
|
|
608
|
+
}}}
|
|
609
|
+
|
|
610
|
+
### Exercise 4: Create a Helper Function {{{
|
|
611
|
+
|
|
612
|
+
**Goal:** Make a reusable `ask()` function
|
|
613
|
+
|
|
614
|
+
**Your command:**
|
|
615
|
+
```bash
|
|
616
|
+
# Add to shell
|
|
617
|
+
ask() {
|
|
618
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
619
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
620
|
+
-H "anthropic-version: 2023-06-01" \
|
|
621
|
+
-H "content-type: application/json" \
|
|
622
|
+
-d "{
|
|
623
|
+
\"model\": \"claude-3-5-sonnet-20241022\",
|
|
624
|
+
\"max_tokens\": 512,
|
|
625
|
+
\"messages\": [{\"role\": \"user\", \"content\": \"$1\"}]
|
|
626
|
+
}" | jq -r '.content[0].text'
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
# Test it
|
|
630
|
+
ask "What does git stash do?"
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
**Expected:** Claude explains git stash
|
|
634
|
+
|
|
635
|
+
}}}
|
|
636
|
+
|
|
637
|
+
### Exercise 5: Process a File {{{
|
|
638
|
+
|
|
639
|
+
**Goal:** Send file contents to Claude for analysis
|
|
640
|
+
|
|
641
|
+
**Your command:**
|
|
642
|
+
```bash
|
|
643
|
+
# Create test file
|
|
644
|
+
echo "TODO: refactor this function
|
|
645
|
+
TODO: add error handling
|
|
646
|
+
TODO: write tests" > todos.txt
|
|
647
|
+
|
|
648
|
+
# Ask Claude to count todos
|
|
649
|
+
FILE_CONTENT=$(cat todos.txt)
|
|
650
|
+
curl -s https://api.anthropic.com/v1/messages \
|
|
651
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" \
|
|
652
|
+
-H "anthropic-version: 2023-06-01" \
|
|
653
|
+
-H "content-type: application/json" \
|
|
654
|
+
-d "{
|
|
655
|
+
\"model\": \"claude-3-5-sonnet-20241022\",
|
|
656
|
+
\"max_tokens\": 256,
|
|
657
|
+
\"messages\": [
|
|
658
|
+
{\"role\": \"user\", \"content\": \"Count the TODOs in this file:\n\n$FILE_CONTENT\"}
|
|
659
|
+
]
|
|
660
|
+
}" | jq -r '.content[0].text'
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
**Expected:** Claude says "There are 3 TODOs"
|
|
664
|
+
|
|
665
|
+
}}}
|
|
666
|
+
|
|
667
|
+
}}}
|
|
668
|
+
|
|
669
|
+
## 🛡️ Security Best Practices {{{
|
|
670
|
+
|
|
671
|
+
### Never Hardcode Keys {{{
|
|
672
|
+
|
|
673
|
+
**❌ Bad:**
|
|
674
|
+
```bash
|
|
675
|
+
curl -H "x-api-key: sk-ant-api03-ABC123..." ...
|
|
676
|
+
```
|
|
677
|
+
|
|
678
|
+
**✅ Good:**
|
|
679
|
+
```bash
|
|
680
|
+
curl -H "x-api-key: $ANTHROPIC_API_KEY" ...
|
|
681
|
+
```
|
|
682
|
+
|
|
683
|
+
}}}
|
|
684
|
+
|
|
685
|
+
### Always Check History {{{
|
|
686
|
+
|
|
687
|
+
```bash
|
|
688
|
+
# After using API, verify key isn't exposed
|
|
689
|
+
history | grep "x-api-key"
|
|
690
|
+
|
|
691
|
+
# Should only show $ANTHROPIC_API_KEY, never actual key
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
}}}
|
|
695
|
+
|
|
696
|
+
### Protect .env Files {{{
|
|
697
|
+
|
|
698
|
+
```bash
|
|
699
|
+
# Always do these together:
|
|
700
|
+
chmod 600 .env # Only you can read
|
|
701
|
+
echo '.env' >> .gitignore # Never commit
|
|
702
|
+
```
|
|
703
|
+
|
|
704
|
+
}}}
|
|
705
|
+
|
|
706
|
+
### Rotate Keys Regularly {{{
|
|
707
|
+
|
|
708
|
+
1. Create new key in console.anthropic.com
|
|
709
|
+
2. Update environment variable
|
|
710
|
+
3. Test with new key
|
|
711
|
+
4. Delete old key
|
|
712
|
+
|
|
713
|
+
**Do this every 90 days minimum**
|
|
714
|
+
|
|
715
|
+
}}}
|
|
716
|
+
|
|
717
|
+
### Monitor Usage {{{
|
|
718
|
+
|
|
719
|
+
Check your usage in Anthropic Console:
|
|
720
|
+
- Track API calls per day
|
|
721
|
+
- Monitor costs
|
|
722
|
+
- Set budget alerts
|
|
723
|
+
- Revoke suspicious keys immediately
|
|
724
|
+
|
|
725
|
+
}}}
|
|
726
|
+
|
|
727
|
+
}}}
|
|
728
|
+
|
|
729
|
+
## 📚 Quick Reference {{{
|
|
730
|
+
|
|
731
|
+
### Essential Headers {{{
|
|
732
|
+
|
|
733
|
+
```bash
|
|
734
|
+
-H "x-api-key: $ANTHROPIC_API_KEY" # Authentication (required)
|
|
735
|
+
-H "anthropic-version: 2023-06-01" # API version (required)
|
|
736
|
+
-H "content-type: application/json" # JSON request (required)
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
}}}
|
|
740
|
+
|
|
741
|
+
### Request Structure {{{
|
|
742
|
+
|
|
743
|
+
```json
|
|
744
|
+
{
|
|
745
|
+
"model": "claude-3-5-sonnet-20241022", // Which model
|
|
746
|
+
"max_tokens": 1024, // Max response length
|
|
747
|
+
"messages": [ // Conversation
|
|
748
|
+
{
|
|
749
|
+
"role": "user", // Always "user" for your input
|
|
750
|
+
"content": "Your question here" // Your actual prompt
|
|
751
|
+
}
|
|
752
|
+
]
|
|
753
|
+
}
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
}}}
|
|
757
|
+
|
|
758
|
+
### Response Structure {{{
|
|
759
|
+
|
|
760
|
+
```json
|
|
761
|
+
{
|
|
762
|
+
"content": [
|
|
763
|
+
{
|
|
764
|
+
"text": "Claude's response here" // Extract this
|
|
765
|
+
}
|
|
766
|
+
],
|
|
767
|
+
"model": "claude-3-5-sonnet-20241022",
|
|
768
|
+
"usage": {
|
|
769
|
+
"input_tokens": 12, // What you sent
|
|
770
|
+
"output_tokens": 45 // What Claude replied
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
```
|
|
774
|
+
|
|
775
|
+
}}}
|
|
776
|
+
|
|
777
|
+
### Extract Text Patterns {{{
|
|
778
|
+
|
|
779
|
+
```bash
|
|
780
|
+
# With jq (recommended)
|
|
781
|
+
| jq -r '.content[0].text'
|
|
782
|
+
|
|
783
|
+
# With grep + sed (no jq needed)
|
|
784
|
+
| grep -o '"text":"[^"]*"' | sed 's/"text":"\(.*\)"/\1/'
|
|
785
|
+
```
|
|
786
|
+
|
|
787
|
+
}}}
|
|
788
|
+
|
|
789
|
+
}}}
|
|
790
|
+
|
|
791
|
+
## 💡 Pro Tips {{{
|
|
792
|
+
|
|
793
|
+
1. **Save common prompts as functions:**
|
|
794
|
+
```bash
|
|
795
|
+
explain() { ask "Explain this command: $1"; }
|
|
796
|
+
explain "git rebase -i HEAD~3"
|
|
797
|
+
```
|
|
798
|
+
|
|
799
|
+
2. **Use system messages for context:**
|
|
800
|
+
```bash
|
|
801
|
+
# Add system role for specialized behavior
|
|
802
|
+
"messages": [
|
|
803
|
+
{"role": "user", "content": "You are a bash expert. Answer concisely."},
|
|
804
|
+
{"role": "assistant", "content": "Understood. I'll provide concise bash help."},
|
|
805
|
+
{"role": "user", "content": "How do I find large files?"}
|
|
806
|
+
]
|
|
807
|
+
```
|
|
808
|
+
|
|
809
|
+
3. **Chain with other commands:**
|
|
810
|
+
```bash
|
|
811
|
+
# Get AI summary of git log
|
|
812
|
+
git log --oneline -10 | ask "Summarize these git commits"
|
|
813
|
+
```
|
|
814
|
+
|
|
815
|
+
4. **Save responses for later:**
|
|
816
|
+
```bash
|
|
817
|
+
ask "Best practices for bash scripting" > bash-tips.txt
|
|
818
|
+
```
|
|
819
|
+
|
|
820
|
+
5. **Add to cron for scheduled AI tasks:**
|
|
821
|
+
```bash
|
|
822
|
+
# Daily summary of system logs
|
|
823
|
+
0 9 * * * curl -s ... | mail -s "Daily Summary" user@example.com
|
|
824
|
+
```
|
|
825
|
+
|
|
826
|
+
}}}
|
|
827
|
+
|
|
828
|
+
## 🚀 Next Steps {{{
|
|
829
|
+
|
|
830
|
+
**Master these progressively:**
|
|
831
|
+
|
|
832
|
+
1. ✅ **Level 1:** Basic API call with environment variable
|
|
833
|
+
2. ✅ **Level 2:** Extract text with jq
|
|
834
|
+
3. ✅ **Level 3:** Create `ask()` helper function
|
|
835
|
+
4. 🎯 **Level 4:** Process files through Claude
|
|
836
|
+
5. 🎯 **Level 5:** Multi-turn conversations
|
|
837
|
+
6. 🎯 **Level 6:** Integrate into shell scripts
|
|
838
|
+
7. 🎯 **Level 7:** Build custom CLI tools
|
|
839
|
+
|
|
840
|
+
**Related skills:**
|
|
841
|
+
- Learn jq for JSON manipulation
|
|
842
|
+
- Master curl flags and options
|
|
843
|
+
- Study bash functions and aliases
|
|
844
|
+
- Explore API rate limiting and costs
|
|
845
|
+
|
|
846
|
+
**Resources:**
|
|
847
|
+
- Anthropic API docs: https://docs.anthropic.com
|
|
848
|
+
- Pricing: https://www.anthropic.com/pricing
|
|
849
|
+
- API Console: https://console.anthropic.com
|
|
850
|
+
|
|
851
|
+
}}}
|
|
852
|
+
|
|
853
|
+
---
|
|
854
|
+
|
|
855
|
+
**Remember:** Claude API in your terminal = AI assistance without breaking flow! 🚀
|