Authentication overview
Mempool provides real-time data on the Bitcoin and Lightning Networks, primarily through its public web interface and a comprehensive API. The primary public Mempool instance, accessible via mempool.space, offers extensive data without requiring explicit authentication for general browsing or most read-only API calls. This model prioritizes open access to Bitcoin network information, enabling developers and users to monitor transactions, blocks, and network statistics freely.
For users operating a self-hosted Mempool node, authentication requirements and mechanisms can be configured based on the deployment's specific needs. Self-hosting allows for greater control over data access, resource allocation, and security policies, which may include implementing API keys or other access controls to restrict who can query the local instance or perform administrative actions. The default public API endpoints are generally designed for broad accessibility.
Understanding the distinction between the public Mempool service and self-hosted deployments is crucial for implementing appropriate authentication strategies. While the public API is largely unauthenticated by design for most data retrieval, specific use cases, such as custom integrations requiring rate limit adjustments or certain administrative functions on a private instance, might necessitate credential management.
Supported authentication methods
Mempool's public API primarily operates without explicit authentication for read-only access to its core data. This design choice aligns with the open-source nature of Bitcoin data and facilitates broad integration. However, for specific advanced use cases, especially with self-hosted instances or future premium services, authentication methods can be employed. The most common methods, particularly for controlled access to self-hosted nodes or developer-specific endpoints, involve API keys or token-based authentication.
Public Mempool.space Instance
- No Authentication (Default): Most API endpoints for the public instance at
api.mempool.spacedo not require any authentication headers or parameters. This allows for straightforward data retrieval for blocks, transactions, addresses, and network statistics.
Self-Hosted Mempool Nodes
When deploying a self-hosted Mempool node, administrators have the flexibility to implement various authentication and authorization layers. These are typically configured at the reverse proxy level or within the application's environment:
- API Keys: A common method involves generating unique alphanumeric strings (API keys) that clients include in their request headers or as query parameters. The server then validates this key against a list of authorized keys to grant access. API keys offer a balance of security and ease of implementation for machine-to-machine communication.
- Bearer Tokens (e.g., JWT): For more dynamic sessions, self-hosted instances can integrate token-based authentication. After a user or service authenticates (e.g., with a username and password), a server issues a token (like a JSON Web Token or JWT) that the client includes in subsequent requests. This token proves their authenticated status for a limited period. The IETF's RFC 6750 defines the Bearer Token usage in HTTP.
- IP Whitelisting: Restricting API access to a predefined list of trusted IP addresses. While not a primary authentication method, it serves as an effective authorization layer, particularly for internal services or known partners accessing a private Mempool node.
- Basic Authentication: Though less common for modern APIs, HTTP Basic Auth (username and password in the
Authorizationheader) can be configured, especially for internal tools or smaller deployments of a self-hosted node.
Here's a comparison of common authentication methods:
| Method | When to use | Security Level |
|---|---|---|
| No Authentication | Accessing public, read-only data from mempool.space | Low (no identity verification) |
| API Keys | Controlled access to self-hosted node; machine-to-machine integrations | Medium (key secrecy is critical) |
| Bearer Tokens (JWT) | Dynamic sessions on self-hosted node; user-based access | High (short-lived, signed tokens) |
| IP Whitelisting | Restricting access to known networks/servers for self-hosted node | Medium (depends on network security) |
Getting your credentials
For the public Mempool API (api.mempool.space), no specific credentials are required for most read-only operations. You can initiate requests directly to the API endpoints as documented in the Mempool API documentation.
If you are operating a self-hosted Mempool node and wish to implement authentication, the process for obtaining or generating credentials is within your control as the administrator:
For API Keys (Self-Hosted)
- Configuration File: API keys are typically defined within the configuration files of your Mempool node deployment or the reverse proxy (e.g., Nginx, Caddy) that sits in front of it. You would manually generate a strong, random string to serve as the key.
- Environmental Variables: For containerized deployments (Docker), API keys can be passed as environmental variables, allowing for easier management and rotation without modifying core application files.
- Database (Advanced): In more complex self-hosted setups, API keys might be stored and managed within a database, often associated with specific user accounts or service roles. This requires custom development to integrate with Mempool's core functionality or a separate access management layer.
For Bearer Tokens (Self-Hosted)
Implementing bearer tokens (like JWTs) requires a more involved setup, often leveraging an authentication server or library:
- User/Service Registration: Define users or services that will require authenticated access to your self-hosted Mempool node.
- Token Issuance Endpoint: Set up an endpoint where users/services can exchange their primary credentials (e.g., username/password, client ID/secret) for a short-lived bearer token.
- Token Validation: Configure your Mempool node or its proxy to validate incoming bearer tokens (e.g., checking signature, expiration, issuer). This often involves integrating with an OpenID Connect (OIDC) provider or a custom identity server.
Consult the documentation for your chosen reverse proxy or authentication framework (e.g., Kong Gateway authentication, Cloudflare Access integrations) for specific steps on how to configure and manage credentials for your self-hosted Mempool setup.
Authenticated request example
Since the public Mempool API does not typically require authentication for read-only access, a standard request looks like this:
Unauthenticated Public API Request Example (cURL)
curl -X GET "https://mempool.space/api/v1/blocks/tip/height"
This request retrieves the height of the current block without any authentication headers.
Self-Hosted API Key Authenticated Request Example (cURL)
If you have configured your self-hosted Mempool node with an API key (e.g., X-API-KEY header), an authenticated request would typically include this key. Replace YOUR_API_KEY_HERE with your actual key:
curl -X GET \
-H "X-API-KEY: YOUR_API_KEY_HERE" \
"https://your-self-hosted-mempool.com/api/v1/blocks/tip/height"
The specific header name (e.g., X-API-KEY, Authorization: Bearer) and its expected format will depend on how you configure authentication on your self-hosted instance.
Self-Hosted Bearer Token Authenticated Request Example (cURL)
For a self-hosted instance configured to use Bearer tokens (e.g., a JWT), the request would look like this:
curl -X GET \
-H "Authorization: Bearer YOUR_BEARER_TOKEN_HERE" \
"https://your-self-hosted-mempool.com/api/v1/some-protected-endpoint"
In this example, YOUR_BEARER_TOKEN_HERE would be the token obtained after successful authentication.
Security best practices
When integrating with Mempool, particularly when dealing with self-hosted instances and potentially authenticated endpoints, adhering to security best practices is essential to protect your application and data.
- Use HTTPS/TLS for all API Communication: Always ensure that all communication with Mempool APIs, especially self-hosted ones, occurs over HTTPS. This encrypts data in transit, protecting against eavesdropping and man-in-the-middle attacks. Public Mempool instances already enforce HTTPS.
- Secure API Keys/Tokens: If using API keys or bearer tokens for self-hosted instances:
- Never embed keys directly in source code. Use environment variables, secure configuration files, or a secrets management service.
- Restrict key permissions. Grant only the minimum necessary privileges to each key.
- Rotate keys regularly. Establish a schedule for rotating API keys to minimize the impact if a key is compromised.
- Avoid logging keys/tokens. Ensure that API keys or tokens are not exposed in application logs or error messages.
- Implement Rate Limiting (Self-Hosted): For self-hosted instances, configure rate limiting to prevent abuse, DDoS attacks, and resource exhaustion. This can be done at the reverse proxy level or within your application logic.
- Validate and Sanitize Inputs: Although Mempool's API is primarily read-only, always validate and sanitize any input your application sends to prevent injection vulnerabilities.
- Principle of Least Privilege: Design your applications and configure your self-hosted Mempool node to operate with the minimum necessary permissions.
- Monitor API Usage: Keep track of API calls and potential anomalies. Unusual request patterns might indicate a security incident or misuse of credentials.
- Keep Dependencies Updated: Regularly update libraries, frameworks, and your Mempool node software to patch known vulnerabilities. This includes the underlying operating system and any supporting services.
- Implement Strong Access Controls (Self-Hosted): If your self-hosted Mempool instance serves multiple users or applications, establish robust access controls to ensure that each entity can only access authorized data or functions. This might involve role-based access control (RBAC) mechanisms.
- Review Security Documentation: Refer to general API security guidelines and best practices from reputable sources, such as those provided by OWASP API Security Project, to continuously enhance your security posture.