Appearance
cURL Examples
Learn how to use SheetsToJson with cURL commands for testing and scripting.
Setup
Export your credentials as environment variables for easy reuse:
bash
export API_KEY="your_api_key_here"
export SHEET_ID="your_sheet_id"
export BASE_URL="https://api.sheetstojson.com"Basic Operations
Fetch All Rows
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users"With pagination:
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users?limit=50&offset=0"With sorting:
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users?sort=created_at&order=desc"With filtering:
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users?filter=status=active"Fetch Single Row
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users/123"Create Row
Single row:
bash
curl -X POST \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "Alice Johnson",
"email": "[email protected]",
"role": "admin"
}' \
"$BASE_URL/api/v1/$SHEET_ID/Users"Multiple rows:
bash
curl -X POST \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '[
{"name": "Alice Johnson", "email": "[email protected]"},
{"name": "Bob Smith", "email": "[email protected]"}
]' \
"$BASE_URL/api/v1/$SHEET_ID/Users"From a file:
bash
curl -X POST \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d @user.json \
"$BASE_URL/api/v1/$SHEET_ID/Users"Update Row
bash
curl -X PUT \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"role": "user",
"updated_at": "2025-01-20T10:30:00Z"
}' \
"$BASE_URL/api/v1/$SHEET_ID/Users/123"Delete Row
bash
curl -X DELETE \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users/123"Schema Operations
Get Schema
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/$SHEET_ID/schema"Refresh Schema
bash
curl -X POST \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/$SHEET_ID/refresh-schema"Pretty Print JSON
Use jq for formatted output:
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | jqExtract specific fields:
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | jq '.data[] | {name, email}'Count rows:
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | jq '.data | length'Verbose Output
Show request headers and response details:
bash
curl -v \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users"View only headers:
bash
curl -I \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users"Check Rate Limits
bash
curl -I \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | grep -i ratelimitOutput:
X-RateLimit-Limit: 10000
X-RateLimit-Remaining: 9847
X-RateLimit-Reset: 1705795200Save Response to File
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" \
-o users.jsonError Handling
Show HTTP status code:
bash
curl -w "\nHTTP Status: %{http_code}\n" \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users"Fail on error (exit code != 0):
bash
curl -f \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" || echo "Request failed"Batch Operations
Create Multiple Users from CSV
bash
# Convert CSV to JSON
cat users.csv | \
jq -Rs 'split("\n") | .[1:] | map(split(",")) |
map({name: .[0], email: .[1], role: .[2]})' | \
curl -X POST \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d @- \
"$BASE_URL/api/v1/$SHEET_ID/Users"Update Multiple Rows
bash
#!/bin/bash
# update_users.sh - Update multiple users
for user_id in 101 102 103 104; do
echo "Updating user $user_id..."
curl -X PUT \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"status": "active",
"updated_at": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"
}' \
"$BASE_URL/api/v1/$SHEET_ID/Users/$user_id"
echo ""
donePagination Script
Fetch all rows across multiple pages:
bash
#!/bin/bash
# fetch_all_users.sh
LIMIT=100
OFFSET=0
ALL_USERS="[]"
while true; do
echo "Fetching offset $OFFSET..."
response=$(curl -s \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users?limit=$LIMIT&offset=$OFFSET")
users=$(echo "$response" | jq -r '.data')
count=$(echo "$users" | jq 'length')
# Merge results
ALL_USERS=$(echo "$ALL_USERS $users" | jq -s 'add')
if [ "$count" -lt "$LIMIT" ]; then
break
fi
OFFSET=$((OFFSET + LIMIT))
done
echo "Total users fetched: $(echo "$ALL_USERS" | jq 'length')"
echo "$ALL_USERS" > all_users.jsonSearch and Filter
Find users by email
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | \
jq '.data[] | select(.email | contains("example.com"))'Count active users
bash
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users?filter=status=active" | \
jq '.meta.total'Get users created today
bash
TODAY=$(date +%Y-%m-%d)
curl -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | \
jq --arg today "$TODAY" '.data[] | select(.created_at | startswith($today))'Monitoring Script
Monitor API health and usage:
bash
#!/bin/bash
# monitor_api.sh
check_api() {
response=$(curl -s -w "\n%{http_code}" \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users?limit=1")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n-1)
if [ "$http_code" -eq 200 ]; then
remaining=$(curl -I -s \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | \
grep -i "x-ratelimit-remaining" | \
awk '{print $2}' | tr -d '\r')
echo "✓ API is healthy. Rate limit remaining: $remaining"
else
echo "✗ API error. HTTP $http_code"
echo "$body" | jq -r '.message'
fi
}
# Run check
check_api
# Or run continuously
# while true; do
# check_api
# sleep 300 # Check every 5 minutes
# doneSync to Local Database
Sync Google Sheets data to SQLite:
bash
#!/bin/bash
# sync_to_sqlite.sh
DB_FILE="local.db"
# Create table if not exists
sqlite3 "$DB_FILE" <<EOF
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
name TEXT,
email TEXT,
role TEXT,
synced_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
EOF
# Fetch data from API
curl -s -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users" | \
jq -r '.data[] | [.id, .name, .email, .role] | @tsv' | \
while IFS=$'\t' read -r id name email role; do
sqlite3 "$DB_FILE" <<EOF
INSERT OR REPLACE INTO users (id, name, email, role)
VALUES ('$id', '$name', '$email', '$role');
EOF
done
echo "Sync complete. Total rows:"
sqlite3 "$DB_FILE" "SELECT COUNT(*) FROM users;"Testing with Different HTTP Methods
Test all CRUD operations
bash
#!/bin/bash
# test_crud.sh
echo "=== Testing CRUD Operations ==="
# CREATE
echo "1. Creating user..."
CREATE_RESPONSE=$(curl -s -X POST \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{"name":"Test User","email":"[email protected]"}' \
"$BASE_URL/api/v1/$SHEET_ID/Users")
USER_ID=$(echo "$CREATE_RESPONSE" | jq -r '.data.rows[0].id')
echo " Created user ID: $USER_ID"
# READ
echo "2. Reading user..."
curl -s -H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users/$USER_ID" | \
jq -r '.data | {name, email}'
# UPDATE
echo "3. Updating user..."
curl -s -X PUT \
-H "X-API-Key: $API_KEY" \
-H "Content-Type: application/json" \
-d '{"role":"admin"}' \
"$BASE_URL/api/v1/$SHEET_ID/Users/$USER_ID" | \
jq -r '.data.role'
# DELETE
echo "4. Deleting user..."
curl -s -X DELETE \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users/$USER_ID" | \
jq -r '.message'
echo "=== Test Complete ==="Performance Testing
Measure response time
bash
curl -w "\nTime: %{time_total}s\n" \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users"Run load test with Apache Bench
bash
# Install: apt-get install apache2-utils
ab -n 100 -c 10 \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users"Debugging
Trace entire request/response
bash
curl -v --trace-ascii - \
-H "X-API-Key: $API_KEY" \
"$BASE_URL/api/v1/$SHEET_ID/Users"Test with different API keys
bash
# Test with invalid key
curl -H "X-API-Key: invalid_key" \
"$BASE_URL/api/v1/$SHEET_ID/Users"
# Should return 401 UnauthorizedCheck endpoint availability
bash
curl -I "$BASE_URL/api/v1/$SHEET_ID/Users"
