Authentication
Secure your API requests with authentication keys
API Key Security
- Never commit API keys to version control
- Use environment variables to store keys
- Rotate keys regularly for security
- Use different keys for development and production
Getting Your API Key
To get your API key:
- Log in to your ScrapeHub dashboard
- Navigate to Settings → API Keys
- Click "Generate New Key"
- Give your key a descriptive name (e.g., "Production", "Development")
- Copy and securely store your key
Important
Your API key will only be shown once. If you lose it, you'll need to generate a new one.
Authentication Methods
Header Authentication (Recommended)
Pass your API key in the X-API-KEY header:
Terminalcurl -X POST https://api.scrapehub.io/v4/scrape \ -H "X-API-KEY: sk_live_xxxx_449x" \ -H "Content-Type: application/json" \ -d '{"target": "https://example.com"}'
Using Environment Variables
Python
example.py
import os
from scrapehub import ScrapeHubClient
# Store API key in environment
api_key = os.getenv('SCRAPEHUB_API_KEY')
client = ScrapeHubClient(api_key=api_key)
result = client.scrape(
url='https://example.com',
engine='neural-x1'
)Set the environment variable:
Terminalexport SCRAPEHUB_API_KEY="sk_live_xxxx_449x"
Node.js
example.js
const { ScrapeHubClient } = require('@scrapehub/node');
// Load from .env file
require('dotenv').config();
const client = new ScrapeHubClient({
apiKey: process.env.SCRAPEHUB_API_KEY
});
async function scrape() {
const result = await client.scrape({
url: 'https://example.com',
engine: 'neural-x1'
});
console.log(result.data);
}
scrape();Create a .env file:
.env
TerminalSCRAPEHUB_API_KEY=sk_live_xxxx_449x
API Key Types
Live Keys
Format: sk_live_xxxx_xxxx
Use in production. Counted towards your plan limits.
Test Keys
Format: sk_test_xxxx_xxxx
Use for testing. Limited functionality, no charges.
Error Handling
Authentication Errors
401 UnauthorizedMissing or invalid API keyYour API key is missing or incorrect. Verify your key and try again.
403 ForbiddenAPI key lacks permissionsYour API key doesn't have permission for this operation.
429 Too Many RequestsRate limit exceededYou've exceeded your rate limit. Wait before making more requests.
Handling Authentication Errors
error_handling.py
from scrapehub import ScrapeHubClient
from scrapehub.exceptions import AuthenticationError
client = ScrapeHubClient(api_key="your_api_key")
try:
result = client.scrape("https://example.com")
except AuthenticationError as e:
print(f"Authentication failed: {e}")
print("Please check your API key")
except Exception as e:
print(f"Error: {e}")