Authentication overview

Restpack's API authentication system is designed to provide secure access to its suite of web scraping and data extraction services. The primary method for authenticating requests is through the use of an API key. This key serves as a unique identifier for your account and is essential for making any authenticated calls to Restpack's various APIs, such as the Scraping API, SERP API, and Screenshots API. Without a valid API key, requests will be rejected, preventing unauthorized usage and ensuring resource allocation is properly managed per user account.

The API key model is a common approach for authenticating programmatic access to web services, offering a balance between ease of implementation and security for many use cases. It allows developers to quickly integrate Restpack's functionalities into their applications while providing a mechanism for tracking usage and enforcing rate limits. Users are responsible for safeguarding their API keys to prevent unauthorized access to their accounts and potential misuse of their allocated request quotas.

Supported authentication methods

Restpack exclusively supports API key authentication for its services. This method involves including a unique, secret key with each API request. The key is typically passed as a query parameter in the request URL. This approach is widely adopted for its simplicity and effectiveness in securing access to various web services.

API Key Authentication

API key authentication is a token-based method where a unique string (the API key) is used to identify the calling user or application. When a request is made to Restpack's API, the system checks for the presence and validity of this key. If the key is valid, the request is processed, and usage is attributed to the corresponding account. If the key is missing or invalid, the request is denied.

This method is suitable for most server-to-server communication and applications where the API key can be securely stored and transmitted. For client-side applications, additional precautions are necessary to prevent the exposure of the API key, as direct exposure could lead to unauthorized use.

Comparison of Authentication Methods

While Restpack currently focuses on API key authentication, it's useful to understand its position relative to other common API authentication schemes. The following table provides a general overview:

Method When to Use Security Level Key Management
API Key (Restpack) Server-to-server communication, internal tools, applications where key can be secured. Standard User-managed via dashboard.
OAuth 2.0 Third-party applications, delegated authorization, user consent flows. High Managed by OAuth provider, tokens refreshed.
Basic HTTP Authentication Simple internal APIs, low-risk scenarios (often deprecated for APIs). Low (requires HTTPS) Application-managed.
Bearer Tokens (JWT) Stateless APIs, microservices, mobile applications. High Managed by client and server, often short-lived.

For more details on general API security practices, refer to resources like the Microsoft Azure API security best practices, which cover various authentication strategies.

Getting your credentials

To access Restpack's APIs, you first need to obtain an API key. This key is generated and managed through your Restpack account dashboard. The process typically involves signing up for an account and then navigating to the API settings section.

Step-by-step guide to obtaining your API key:

  1. Sign Up or Log In: Go to the Restpack homepage and either sign up for a new account or log in to an existing one. Restpack offers a free tier that includes 5,000 requests per month, which is sufficient for initial testing and development.
  2. Access Dashboard: Once logged in, you will be redirected to your Restpack user dashboard.
  3. Locate API Key Section: Navigate to the section of the dashboard dedicated to API keys or general API settings. The exact location may vary, but it is typically found under 'Settings', 'API Access', or a similar label.
  4. Generate Key: If you do not already have an API key, there will be an option to generate a new one. Click this button to create your unique API key.
  5. Copy Key: Once generated, your API key will be displayed. It is crucial to copy this key immediately and store it securely. Restpack's documentation emphasizes the importance of keeping your API key confidential to prevent unauthorized use of your account and resources.
  6. Revoke/Regenerate (Optional): Your dashboard will also provide options to revoke an existing API key or generate a new one. This functionality is useful if your key is compromised or if you need to rotate keys for security reasons.

Your API key is a long, alphanumeric string. Treat it like a password. Do not embed it directly into client-side code that could be publicly exposed, such as JavaScript in a web browser. Instead, use it in server-side applications or secure environments.

Authenticated request example

Once you have your API key, you can include it in your API requests. For Restpack, the API key is typically passed as a query parameter named api_key.

Example using cURL (Scraping API)

This cURL example demonstrates how to make a request to the Restpack Scraping API to retrieve the HTML content of a webpage, including your API key:

curl -X GET \
  'https://api.restpack.io/v2/scraping/html?api_key=YOUR_API_KEY&url=https://example.com' \
  -H 'Content-Type: application/json'

In this example, replace YOUR_API_KEY with the actual API key you obtained from your Restpack dashboard. The url parameter specifies the target webpage to scrape.

Example using Python (Requests library)

Here's how you would make a similar request using Python's popular requests library:

import requests

api_key = "YOUR_API_KEY"  # Replace with your actual API key
target_url = "https://example.com"

params = {
    "api_key": api_key,
    "url": target_url
}

response = requests.get("https://api.restpack.io/v2/scraping/html", params=params)

if response.status_code == 200:
    print("Scraped HTML:")
    print(response.text)
else:
    print(f"Error: {response.status_code} - {response.text}")

This Python script constructs the parameters dictionary, including the api_key, and then makes a GET request to the Restpack Scraping API endpoint. The response contains the HTML content if successful.

Example using Node.js (axios library)

For Node.js developers, using a library like axios is a common approach:

const axios = require('axios');

const apiKey = "YOUR_API_KEY"; // Replace with your actual API key
const targetUrl = "https://example.com";

axios.get('https://api.restpack.io/v2/scraping/html', {
  params: {
    api_key: apiKey,
    url: targetUrl
  }
})
.then(response => {
  console.log('Scraped HTML:', response.data);
})
.catch(error => {
  console.error('Error:', error.response ? error.response.data : error.message);
});

These examples demonstrate the fundamental principle of including your api_key as a query parameter in your requests to Restpack's APIs, as detailed in the Restpack documentation.

Security best practices

Securing your Restpack API key is crucial to prevent unauthorized access to your account and services. Adhering to best practices for API key management helps protect your data, control your usage, and maintain the integrity of your applications.

1. Keep your API key confidential

  • Do not hardcode keys in public repositories: Never embed your API key directly into client-side code (e.g., JavaScript in a browser) or commit it to public version control systems like GitHub. Use environment variables or a secure configuration management system instead.
  • Server-side usage: Always use your API key from your backend servers or trusted environments where the key cannot be easily intercepted or accessed by unauthorized parties.
  • Access control: Limit access to your API keys only to personnel or systems that absolutely require it.

2. Use environment variables

Instead of directly embedding your API key in your code, store it as an environment variable. This allows you to keep the key out of your codebase and easily change it without modifying application logic. Many development frameworks and deployment platforms support environment variables.

# Example of setting an environment variable
export RESTPACK_API_KEY="YOUR_API_KEY"
# Example of accessing an environment variable in Python
import os
api_key = os.environ.get("RESTPACK_API_KEY")

3. Regularly rotate API keys

Periodically generating a new API key and revoking the old one reduces the risk associated with a compromised key. The frequency of rotation depends on your security policies and risk assessment. Restpack's dashboard allows you to regenerate your API key.

4. Monitor API usage

Regularly check your Restpack dashboard for unusual activity or spikes in API usage. This can help you detect potential unauthorized use of your API key early. If you notice suspicious activity, immediately revoke your current key and generate a new one.

5. Implement rate limiting and quotas (where applicable)

While Restpack enforces its own rate limits, if you are building an application that uses the Restpack API, consider implementing client-side rate limiting or usage quotas to prevent accidental over-consumption of your API credits, which could also indicate a compromised key.

6. Secure your development environment

Ensure that your development machines and deployment servers are secure. Use strong passwords, enable multi-factor authentication, and keep software updated to protect against vulnerabilities that could expose your API keys.

7. Use HTTPS for all communications

All interactions with Restpack's API should occur over HTTPS. This encrypts the communication channel, protecting your API key and data from interception during transit. Restpack's API endpoints are inherently served over HTTPS. For a deeper understanding of secure communication, review resources on secure contexts in web development.

By following these best practices, developers can significantly enhance the security posture of their applications integrating with Restpack's services.