cURL Examples
This page provides comprehensive cURL examples for interacting with the Harvest Context API directly from the command line. These examples are useful for testing, debugging, and integrating with shell scripts.Basic Examples
List All Indexes
Copy
curl -X GET \
"https://www.goharvest.ai/api/v1/developer/list-indexes" \
-H "API-Key: ${HARVEST_API_KEY}"
Get Context
Copy
curl -X POST \
"https://www.goharvest.ai/api/v1/developer/get-context" \
-H "API-Key: ${HARVEST_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"query": "how to make authenticated POST request",
"index_id": "scrape_123456"
}'
Complete Workflow Script
This script lists indexes, lets you select one, and queries for context:Copy
#!/bin/bash
# context_query.sh
set -e
if [ -z "$HARVEST_API_KEY" ]; then
echo "Error: HARVEST_API_KEY environment variable is required"
exit 1
fi
BASE_URL="https://www.goharvest.ai/api/v1/developer"
echo "📚 Fetching available indexes..."
# List all indexes
INDEXES_RESPONSE=$(curl -s -X GET \
"${BASE_URL}/list-indexes" \
-H "API-Key: ${HARVEST_API_KEY}")
# Check if request was successful
if ! echo "$INDEXES_RESPONSE" | jq -e '.ok' > /dev/null 2>&1; then
echo "❌ Failed to fetch indexes"
echo "Response: $INDEXES_RESPONSE"
exit 1
fi
# Extract and display indexes
INDEXES=$(echo "$INDEXES_RESPONSE" | jq -r '.data.indexes')
INDEX_COUNT=$(echo "$INDEXES" | jq 'length')
echo "✅ Found ${INDEX_COUNT} indexed services:"
echo ""
# Display indexes with numbers
echo "$INDEXES" | jq -r 'to_entries | .[] | "\(.key + 1). \(.value.name) (\(.value.type))"'
# Get user selection (or use first one if non-interactive)
if [ -n "$1" ]; then
# Non-interactive mode: use first index and provided query
QUERY="$1"
SELECTION=0
else
# Interactive mode
echo ""
read -p "Select a service (1-${INDEX_COUNT}): " SELECTION
SELECTION=$((SELECTION - 1))
echo ""
read -p "Enter your query: " QUERY
fi
# Get selected index_id
INDEX_ID=$(echo "$INDEXES" | jq -r ".[$SELECTION].index_id")
INDEX_NAME=$(echo "$INDEXES" | jq -r ".[$SELECTION].name")
if [ -z "$INDEX_ID" ] || [ "$INDEX_ID" = "null" ]; then
echo "❌ Invalid selection"
exit 1
fi
echo ""
echo "🔍 Querying ${INDEX_NAME} for: \"${QUERY}\""
echo ""
# Get context
CONTEXT_RESPONSE=$(curl -s -X POST \
"${BASE_URL}/get-context" \
-H "API-Key: ${HARVEST_API_KEY}" \
-H "Content-Type: application/json" \
-d "{
\"query\": \"${QUERY}\",
\"index_id\": \"${INDEX_ID}\"
}")
# Check if request was successful
if ! echo "$CONTEXT_RESPONSE" | jq -e '.ok' > /dev/null 2>&1; then
echo "❌ Failed to get context"
echo "Response: $CONTEXT_RESPONSE"
exit 1
fi
# Extract and display context
CONTEXT=$(echo "$CONTEXT_RESPONSE" | jq -r '.data.context')
echo "================================================================================"
echo "CONTEXT:"
echo "================================================================================"
echo "$CONTEXT"
echo "================================================================================"
echo ""
echo "✅ Query completed!"
Copy
chmod +x context_query.sh
# Interactive mode
./context_query.sh
# Non-interactive mode with query
./context_query.sh "how to handle authentication"
Advanced: Batch Context Queries
Query multiple services with the same question:Copy
#!/bin/bash
# batch_query.sh
set -e
QUERY="$1"
if [ -z "$QUERY" ]; then
echo "Usage: $0 <query>"
exit 1
fi
if [ -z "$HARVEST_API_KEY" ]; then
echo "Error: HARVEST_API_KEY environment variable is required"
exit 1
fi
BASE_URL="https://www.goharvest.ai/api/v1/developer"
echo "🔍 Querying all services for: \"${QUERY}\""
echo ""
# Get all indexes
INDEXES=$(curl -s -X GET \
"${BASE_URL}/list-indexes" \
-H "API-Key: ${HARVEST_API_KEY}" | jq -r '.data.indexes')
# Query each index
echo "$INDEXES" | jq -c '.[]' | while read -r index; do
INDEX_ID=$(echo "$index" | jq -r '.index_id')
INDEX_NAME=$(echo "$index" | jq -r '.name')
INDEX_TYPE=$(echo "$index" | jq -r '.type')
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📖 ${INDEX_NAME} (${INDEX_TYPE})"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
CONTEXT=$(curl -s -X POST \
"${BASE_URL}/get-context" \
-H "API-Key: ${HARVEST_API_KEY}" \
-H "Content-Type: application/json" \
-d "{
\"query\": \"${QUERY}\",
\"index_id\": \"${INDEX_ID}\"
}" | jq -r '.data.context // "No context found"')
echo "$CONTEXT" | head -20
echo ""
echo "..."
echo ""
done
echo "✅ Batch query completed!"
Saving Results to File
Copy
#!/bin/bash
# save_context.sh
INDEX_ID="$1"
QUERY="$2"
OUTPUT_FILE="${3:-context_output.md}"
if [ -z "$INDEX_ID" ] || [ -z "$QUERY" ]; then
echo "Usage: $0 <index_id> <query> [output_file]"
exit 1
fi
if [ -z "$HARVEST_API_KEY" ]; then
echo "Error: HARVEST_API_KEY environment variable is required"
exit 1
fi
BASE_URL="https://www.goharvest.ai/api/v1/developer"
echo "📝 Saving context to ${OUTPUT_FILE}..."
# Get context and save to file
CONTEXT=$(curl -s -X POST \
"${BASE_URL}/get-context" \
-H "API-Key: ${HARVEST_API_KEY}" \
-H "Content-Type: application/json" \
-d "{
\"query\": \"${QUERY}\",
\"index_id\": \"${INDEX_ID}\"
}" | jq -r '.data.context // "No context found"')
# Write to file with header
cat > "$OUTPUT_FILE" << EOF
# Context Query Results
**Query:** ${QUERY}
**Index ID:** ${INDEX_ID}
**Date:** $(date)
---
${CONTEXT}
EOF
echo "✅ Saved to ${OUTPUT_FILE}"
Copy
chmod +x save_context.sh
./save_context.sh "scrape_123456" "how to authenticate" "auth_guide.md"
Dependencies
These scripts require:curl
- for HTTP requestsjq
- for JSON processingbash
- for shell scripting
Copy
sudo apt-get install curl jq
Copy
brew install curl jq