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:

  1. Log in to your ScrapeHub dashboard
  2. Navigate to Settings → API Keys
  3. Click "Generate New Key"
  4. Give your key a descriptive name (e.g., "Production", "Development")
  5. 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:

Terminal
curl -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
python
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:

Terminal
export SCRAPEHUB_API_KEY="sk_live_xxxx_449x"

Node.js

example.js
javascript
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
bash
Terminal
SCRAPEHUB_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 key

Your API key is missing or incorrect. Verify your key and try again.

403 ForbiddenAPI key lacks permissions

Your API key doesn't have permission for this operation.

429 Too Many RequestsRate limit exceeded

You've exceeded your rate limit. Wait before making more requests.

Handling Authentication Errors

error_handling.py
python
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}")