Skip to main content

cURL Integration

Use cURL for quick testing and debugging without any dependencies.

Basic Request

Simple Chat Completion

curl -X POST http://localhost/api/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello, Enclava!"}]
}'

Pretty JSON Output

curl -X POST http://localhost/api/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}' | jq '.'

Environment Variables

Store your configuration in environment variables:

# Add to ~/.bashrc or ~/.zshrc
export ENCLAVA_URL="http://localhost/api/v1"
export ENCLAVA_API_KEY="YOUR_API_KEY"

# Reload shell
source ~/.bashrc

# Use in requests
curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'

Examples

List Models

curl -X GET $ENCLAVA_URL/models \
-H "Authorization: Bearer $ENCLAVA_API_KEY"

Chat with System Prompt

curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "system",
"content": "You are a helpful, friendly assistant."
},
{
"role": "user",
"content": "What is 2+2?"
}
]
}'

Chat with Temperature

# Low temperature (more focused)
curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "What is the capital of France?"}],
"temperature": 0.2
}'

# High temperature (more creative)
curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Write a creative story"}],
"temperature": 1.5
}'

Multi-turn Conversation

# First message
curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is 2+2?"}
]
}'

# Second message with context
curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": "2+2 equals 4."},
{"role": "user", "content": "What about 5+3?"}
]
}'

Generate Embeddings

curl -X POST $ENCLAVA_URL/embeddings \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-ada-002",
"input": "The quick brown fox jumps over the lazy dog."
}'

Multiple Text Embeddings

curl -X POST $ENCLAVA_URL/embeddings \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-ada-002",
"input": [
"The cat sat on the mat.",
"The dog ran in the park."
]
}'

Max Tokens Limit

curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Tell me about AI"}],
"max_tokens": 100
}'

Stop Sequences

curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Count from 1 to 10"}],
"stop": ["11", "eleven"]
}'

Streaming

Basic Streaming

curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Tell me a story"}],
"stream": true
}'

Parse Streaming Output

curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Write a poem"}],
"stream": true
}' | while read -r line; do
if [[ $line == data:* ]]; then
DATA="${line#data: }"
if [[ "$DATA" == "[DONE]" ]]; then
break
fi
echo "$DATA" | jq -r '.choices[0].delta.content // empty' 2>/dev/null | tr -d '\n'
fi
done

File Input

Using File for Messages

cat > messages.json << EOF
{
"model": "gpt-3.5-turbo",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello from file!"}
]
}
EOF

curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d @messages.json

Using File for Input (Embeddings)

cat > input.txt << EOF
Hello, world!
This is a test document.
EOF

curl -X POST $ENCLAVA_URL/embeddings \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"text-embedding-ada-002\",
\"input\": \"$(cat input.txt)\"
}"

Error Handling

Check HTTP Status

RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" \
-X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer $ENCLAVA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello"}]
}')

if [ "$RESPONSE" -eq 200 ]; then
echo "✓ Request successful"
else
echo "✗ Request failed with status $RESPONSE"
fi

Capture Errors

curl -X POST $ENCLAVA_URL/chat/completions \
-H "Authorization: Bearer invalid_key" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello"}]
}' | jq '.'

Useful cURL Options

OptionDescriptionExample
-sSilent mode-s
-vVerbose output-v
-w "%{http_code}"Output status code only-w "%{http_code}"
-o fileSave response to file-o response.json
-d @fileRead data from file-d @data.json
-H "Header: Value"Add header-H "Content-Type: application/json"

Scripts

Simple Chat Script

#!/bin/bash

ENCLAVA_URL="${ENCLAVA_URL:-http://localhost/api/v1}"
API_KEY="${ENCLAVA_API_KEY}"

chat() {
local message="$1"
curl -s -X POST "$ENCLAVA_URL/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"gpt-3.5-turbo\",
\"messages\": [{\"role\": \"user\", \"content\": \"$message\"}]
}" | jq -r '.choices[0].message.content'
}

chat "$1"

Usage:

./chat.sh "What is the capital of France?"

Batch Request Script

#!/bin/bash

ENCLAVA_URL="${ENCLAVA_URL:-http://localhost/api/v1}"
API_KEY="${ENCLAVA_API_KEY}"

MESSAGES=(
"Hello, how are you?"
"What is AI?"
"Tell me a joke"
)

for msg in "${MESSAGES[@]}"; do
echo "Processing: $msg"
response=$(curl -s -X POST "$ENCLAVA_URL/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d "{
\"model\": \"gpt-3.5-turbo\",
\"messages\": [{\"role\": \"user\", \"content\": \"$msg\"}]
}")
echo "Response: $(echo $response | jq -r '.choices[0].message.content')"
echo "---"
done

Best Practices

  1. Environment Variables: Store URL and API key in environment variables
  2. Pretty Output: Use jq for readable JSON output
  3. Error Checking: Check HTTP status codes
  4. Silent Mode: Use -s for scripts
  5. File Input: Use @file for large requests

Next Steps