Skip to content

Instantly share code, notes, and snippets.

@hasherezade
Created December 5, 2025 23:36
Show Gist options
  • Select an option

  • Save hasherezade/f9ca8b93979e64974e4904b226de6182 to your computer and use it in GitHub Desktop.

Select an option

Save hasherezade/f9ca8b93979e64974e4904b226de6182 to your computer and use it in GitHub Desktop.
A simple hello world script to talk with Claude AI via API
#!/usr/bin/env python3
import os
import requests
import json
API_KEY = os.getenv("ANTHROPIC_API_KEY")
if not API_KEY:
raise ValueError("Please set the ANTHROPIC_API_KEY environment variable.")
url = "https://api.anthropic.com/v1/messages"
headers = {
"x-api-key": API_KEY,
"anthropic-version": "2023-06-01",
"content-type": "application/json"
}
data = {
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello, Claude! Can you tell a joke about programmers?"}
]
}
response = requests.post(url, headers=headers, json=data)
resp = response.json()
# Extract just the text from the content array
text_blocks = [block["text"] for block in resp.get("content", []) if block["type"] == "text"]
# Join them (in case there are multiple text blocks)
assistant_text = "\n".join(text_blocks)
print(assistant_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment