Skip to content
English

Calling with curl

Without installing any SDK, use curl to call 1折AI's /v1/messages and /v1/chat/completions, list models, test caching, and view streaming output.

curl is the fastest way to confirm whether the API key and endpoint actually work. Every command below can be copied directly. First, put your API key into an environment variable:

bash
export ONEZHE_API_KEY="sk-你的Key"

Windows users are advised to run these commands in WSL or Git Bash; for PowerShell syntax, see Quick Start.

bash
curl https://1zheai.com/v1/messages \
-H "x-api-key: $ONEZHE_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-5-5",
"max_tokens": 128,
"messages": [{"role": "user", "content": "ping"}]
}'
bash
curl https://1zheai.com/v1/chat/completions \
-H "Authorization: Bearer $ONEZHE_API_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-5-5",
"messages": [{"role": "user", "content": "ping"}]
}'
bash
curl https://1zheai.com/v1/models \
-H "Authorization: Bearer $ONEZHE_API_KEY"

Add "stream": true and use -N to turn off curl’s buffering so you can see the response arrive chunk by chunk:

bash
curl -N https://1zheai.com/v1/messages \
-H "x-api-key: $ONEZHE_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-5-5",
"max_tokens": 128,
"stream": true,
"messages": [{"role": "user", "content": "从一数到五"}]
}'

For the event format, see Streaming Output.

Send the same request twice in a row; if the usage.cache_read_input_tokens in the second response is greater than 0, it means the cache was hit. The text in system needs to reach the model’s minimum cacheable length. For the full script, see Verify Models and Usage, and for the underlying mechanism, see Prompt Caching.

bash
curl https://1zheai.com/v1/messages \
-H "x-api-key: $ONEZHE_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-5-5",
"max_tokens": 32,
"system": [{
"type": "text",
"text": "把一段至少 512 token、每次都一样的长文本放在这里",
"cache_control": {"type": "ephemeral"}
}],
"messages": [{"role": "user", "content": "ping"}]
}'
bash
curl -i https://1zheai.com/v1/models -H "Authorization: Bearer $ONEZHE_API_KEY"

-i will print the status code and response headers. For the meaning of status codes, see Error Codes and Troubleshooting.