LessTokens Logo
Integrations
Curl

Documentation

Getting Started

  • Overview
  • Account Setup
  • First API Key
  • First Request
  • Understanding Metrics

API Reference

  • Overview
  • Authentication
  • Compressionnew
  • MCP Servernew
  • Errors

Integrations

  • Node.js / TypeScript
  • Python
  • cURL

Resources

  • Best Practices

Need help?

Join our community or contact support for assistance.

Contact Support →

cURL Integration

Use cURL to test and interact with the LessToken API from the command line. Perfect for quick testing, debugging, and shell scripts.

Basic Request

The simplest compression request using cURL:

terminal
curl -X POST https://lesstokens.hive-hub.ai \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Large language models are powerful but expensive. This API helps reduce costs."
  }'

Replace YOUR_API_KEY

Don't forget to replace YOUR_API_KEY with your actual API key from the dashboard.

Request with Options

Customize compression behavior with options:

terminal
curl -X POST https://lesstokens.hive-hub.ai \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Your text here",
    "options": {
      "maxTimeMs": 10000
    }
  }'

Using Environment Variables

Store your API key in environment variables for security:

terminal
# Set API key as environment variable
export LESSTOKEN_API_KEY="lsk_live_your_key_here"

# Use in cURL request
curl -X POST https://lesstokens.hive-hub.ai \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text here"}'
.bashrc or .zshrc
# Add to your shell configuration for persistence
export LESSTOKEN_API_KEY="lsk_live_your_key_here"
export LESSTOKEN_BASE_URL="https://lesstokens.hive-hub.ai"

Formatted Output with jq

Use jq to format and parse JSON responses:

terminal
# Install jq (if not already installed)
# macOS: brew install jq
# Ubuntu: sudo apt-get install jq

# Pretty-print entire response
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text"}' | jq '.'

# Extract specific fields
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text"}' | jq '.compressedPrompt'

# Show only metrics
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text"}' | jq '{
    originalTokens,
    compressedTokens,
    savingsPercentage
  }'

Error Handling

Check HTTP Status Code

terminal
# Get HTTP status code
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text"}' \
  -w "\nHTTP Status: %{http_code}\n" \
  -o /dev/null -s

# Save response and check status
RESPONSE=$(curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text"}' \
  -w "\n%{http_code}" -s)

HTTP_CODE=$(echo "$RESPONSE" | tail -n 1)
BODY=$(echo "$RESPONSE" | sed '$d')

if [ "$HTTP_CODE" -eq 200 ]; then
  echo "Success: $BODY"
else
  echo "Error $HTTP_CODE: $BODY"
fi

Silent Mode with Error Display

terminal
# Show only errors (silent success)
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text"}' \
  -f -s || echo "Request failed"

# Fail on HTTP errors and show error response
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Your text"}' \
  --fail-with-body

Shell Script Example

Complete shell script for compression with error handling:

compress.sh
#!/bin/bash

# LessToken Compression Script
# Usage: ./compress.sh "Your text to compress"

set -e  # Exit on error

# Configuration
API_KEY="${LESSTOKEN_API_KEY}"
BASE_URL="${LESSTOKEN_BASE_URL:-https://lesstokens.hive-hub.ai}"

# Validate API key
if [ -z "$API_KEY" ]; then
  echo "Error: LESSTOKEN_API_KEY environment variable not set"
  exit 1
fi

# Get prompt from argument or stdin
if [ -n "$1" ]; then
  PROMPT="$1"
else
  echo "Enter text to compress (Ctrl+D when done):"
  PROMPT=$(cat)
fi

# Make API request
echo "Compressing..."
RESPONSE=$(curl -X POST "$BASE_URL/compress" \
  -H "X-API-Key: $API_KEY" \
  -H "Content-Type: application/json" \
  -d "{\"prompt\": \"$PROMPT\"}" \
  -w "\n%{http_code}" \
  -s)

# Parse response
HTTP_CODE=$(echo "$RESPONSE" | tail -n 1)
BODY=$(echo "$RESPONSE" | sed '$d')

# Check status
if [ "$HTTP_CODE" -ne 200 ]; then
  ERROR_MSG=$(echo "$BODY" | jq -r '.message // "Unknown error"')
  echo "Error $HTTP_CODE: $ERROR_MSG"
  exit 1
fi

# Extract and display results
COMPRESSED=$(echo "$BODY" | jq -r '.compressedPrompt')
ORIGINAL_TOKENS=$(echo "$BODY" | jq -r '.originalTokens')
COMPRESSED_TOKENS=$(echo "$BODY" | jq -r '.compressedTokens')
SAVINGS=$(echo "$BODY" | jq -r '.savingsPercentage')

echo "✓ Compression successful!"
echo ""
echo "Original tokens: $ORIGINAL_TOKENS"
echo "Compressed tokens: $COMPRESSED_TOKENS"
echo "Savings: $SAVINGS%"
echo ""
echo "Compressed text:"
echo "$COMPRESSED"

# Save to file (optional)
if [ -n "$2" ]; then
  echo "$COMPRESSED" > "$2"
  echo ""
  echo "Saved to: $2"
fi
Usage Examples
# Make script executable
chmod +x compress.sh

# Compress text from argument
./compress.sh "Your text to compress"

# Compress text from stdin
echo "Your text" | ./compress.sh

# Compress and save to file
./compress.sh "Your text" output.txt

# Compress file contents
./compress.sh "$(cat input.txt)" output.txt

Advanced Techniques

Batch Processing

batch_compress.sh
#!/bin/bash

# Process multiple files
for file in inputs/*.txt; do
  echo "Processing: $file"
  
  PROMPT=$(cat "$file")
  OUTPUT="outputs/$(basename "$file")"
  
  curl -X POST https://lesstokens.hive-hub.ai/api/compress \
    -H "X-API-Key: $LESSTOKEN_API_KEY" \
    -H "Content-Type: application/json" \
    -d "{\"prompt\": \"$PROMPT\"}" \
    -s | jq -r '.compressedPrompt' > "$OUTPUT"
  
  echo "✓ Saved to: $OUTPUT"
  
  # Rate limit: wait 1 second between requests
  sleep 1
done

Monitoring Rate Limits

check_rate_limit.sh
# Check rate limit headers
curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "test"}' \
  -i -s | grep -i 'x-ratelimit'

# Parse rate limit info
HEADERS=$(curl -X POST https://lesstokens.hive-hub.ai/api/compress \
  -H "X-API-Key: $LESSTOKEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"prompt": "test"}' \
  -i -s)

LIMIT=$(echo "$HEADERS" | grep -i 'x-ratelimit-limit' | cut -d' ' -f2)
REMAINING=$(echo "$HEADERS" | grep -i 'x-ratelimit-remaining' | cut -d' ' -f2)
RESET=$(echo "$HEADERS" | grep -i 'x-ratelimit-reset' | cut -d' ' -f2)

echo "Rate Limit: $LIMIT"
echo "Remaining: $REMAINING"
echo "Resets at: $(date -r $RESET)"

Best Practices

Use Environment Variables

Store API keys in environment variables, never hardcode

Error Handling

Always check HTTP status codes and handle errors

Rate Limiting

Add delays between requests in batch operations

Use jq for Parsing

Parse JSON responses with jq for robust scripts

Useful cURL Options

-X POST

HTTP method (POST for compression)

-H

Add header (X-API-Key, Content-Type)

-d

Request data/body

-s

Silent mode (no progress bar)

-w

Write out format (e.g., HTTP status code)

-i

Include response headers in output

-f

Fail silently on HTTP errors

--fail-with-body

Show error response body on failure

-o

Write output to file

-v

Verbose mode (debug information)

Next Steps

Ready to integrate cURL into your workflows!