Grok API with cURL
Updated:
Use cURL to test a Grok API Dev key, reproduce an HTTP error without an SDK, and inspect a streaming response. Every example calls the OpenAI-compatible Responses API.
Prepare the API key
Store the key in an environment variable before running the request. This keeps it out of the command body and makes the same example easier to use in a local terminal or CI secret store.
curl --versionexport GROK_API_DEV_KEY="sk-lg-YOUR_API_KEY"Do not print the variable in a shared terminal or CI log. A leaked key should be revoked rather than reused.
Send a Responses API request
The minimal body contains model and input. Content-Type declares JSON, while Authorization sends the key in the OpenAI-compatible Bearer format.
curl --fail-with-body https://api.llm-gate.tech/v1/responses \
-H "Authorization: Bearer $GROK_API_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.5",
"input": "Explain HTTP in two sentences"
}'--fail-with-body returns a non-zero exit code for HTTP 4xx or 5xx and still prints the JSON error body.
Read the JSON response
Check status before using the generated content. The response also contains an ID for diagnostics, typed output items, and usage with input, output, and total tokens.
{
"id": "resp_01abc",
"status": "completed",
"output": [
{
"type": "message",
"role": "assistant",
"content": [
{ "type": "output_text", "text": "HTTP is..." }
]
}
],
"usage": {
"input_tokens": 12,
"output_tokens": 34,
"total_tokens": 46
}
}Do not assume that generated text always lives at one fixed array index. Use an SDK when the application needs typed parsing.
Stream SSE events with cURL
Add stream: true and use -N to disable cURL output buffering. The terminal will show SSE records as they arrive instead of waiting for one final JSON document.
curl -N --fail-with-body https://api.llm-gate.tech/v1/responses \
-H "Authorization: Bearer $GROK_API_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.5",
"input": "Write a short example",
"stream": true
}'The stream ends normally with response.completed. An error event or a closed connection before completion means the response is incomplete.
Inspect HTTP errors
Start debugging with one short input and the smallest valid JSON body. Add -i when you need response headers together with the body.
curl -i --fail-with-body https://api.llm-gate.tech/v1/responses \
-H "Authorization: Bearer $GROK_API_DEV_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "grok-4.5",
"input": "Test request"
}'- 401: verify the Bearer header and GROK_API_DEV_KEY.
- 404: verify /v1/responses and the grok-4.5 model ID.
- 429: check Retry-After when present and reduce request frequency.
- DNS or TLS failure: no HTTP status exists, so check the network and hostname first.