Authentication overview
Algolia secures access to its hosted search service primarily through API keys. These keys are designed to grant specific permissions, ensuring that different parts of an application—such as a front-end search interface versus a back-end indexing process—only have the necessary level of access. All communication with Algolia's API endpoints occurs over HTTPS, providing encryption in transit for all requests and responses Algolia security best practices.
The core principle behind Algolia's authentication system is to clearly delineate responsibilities and minimize the potential impact of a compromised key. Instead of a single master key for all operations, Algolia provides distinct key types with granular access controls (ACLs). This approach aligns with the principle of least privilege, a fundamental security concept that dictates users and applications should only have access to the resources and operations required for their legitimate purpose Microsoft's least privilege administrative models guide.
For developers, understanding the different key types and their appropriate use cases is critical for building secure and robust search experiences. Misuse or over-privileging API keys can lead to security vulnerabilities, such as unauthorized data modification or access to sensitive information.
Supported authentication methods
Algolia exclusively uses API keys for authentication. These keys are classified by their permissions, which dictate the operations they can perform and the indices they can access. The primary types of API keys include:
- Admin API Key: Grants full control over all indices and operations, including indexing, search, settings modification, and analytics. This key should be kept strictly confidential and used only from secure back-end environments.
- Search-Only API Key: Allows read-only access to search operations. This key is safe to embed in front-end applications (e.g., JavaScript in a browser) because it cannot modify index data or settings.
- Write API Key: Enables additions, updates, and deletions of records within an index. This key should be used only from secure back-end environments or trusted server-side processes.
- Usage API Key: Provides access to retrieve usage statistics and analytics data.
- Monitoring API Key: Grants access to retrieve monitoring information about API status and performance.
In addition to these standard keys, Algolia also supports the creation of scoped API keys with custom access control lists (ACLs), specific validity periods, and rate limits. This allows for fine-grained control over permissions, enabling developers to create temporary or highly restricted keys for specific tasks or users.
Comparison of API Key Types
| Method | When to Use | Security Level | Permissions |
|---|---|---|---|
| Admin API Key | Server-side operations (indexing, configuration, full control) | Highest (must be strictly confidential) | search, addObject, deleteObject, deleteIndex, settings, analytics, usage, logs, etc. |
| Search-Only API Key | Client-side applications (web browsers, mobile apps for search) | Lowest (can be public) | search |
| Write API Key | Server-side indexing operations (adding/updating/deleting records) | High (must be confidential) | addObject, deleteObject, editSettings (if specified) |
| Usage API Key | Server-side retrieval of usage data | Medium (should be confidential) | usage |
| Monitoring API Key | Server-side retrieval of API health and performance metrics | Medium (should be confidential) | logs, monitoring |
| Scoped API Keys | Specific, temporary, or user-specific operations (e.g., restricted search, specific index writes) | Configurable (based on ACLs and expiry) | Custom combination of permissions, potentially limited to specific indices or operations |
Getting your credentials
To obtain your Algolia API keys and Application ID, you need access to your Algolia dashboard. Here's a general process:
- Sign Up/Log In: Navigate to the Algolia website and either sign up for a new account or log in to your existing one.
- Access Dashboard: Once logged in, you will be directed to your Algolia dashboard.
- Locate API Keys: In the dashboard's left-hand navigation, find and click on the "API Keys" section.
- Retrieve Keys: On the API Keys page, you will see your Application ID and several default API keys (Admin API Key, Search-Only API Key, Write API Key).
- Generate New Keys (Optional): You can generate additional API keys with custom permissions, validity periods, and rate limits by clicking the "New API Key" button. This allows you to tailor keys to specific use cases, enhancing security.
It is important to note your Application ID, as it is required alongside your API key for every request to identify your Algolia application Algolia Application ID details. When creating new API keys, always configure their Access Control List (ACL) to grant only the minimum necessary permissions for the task they will perform.
Authenticated request example
Algolia provides client libraries (SDKs) for various programming languages, which simplify the process of making authenticated requests. These SDKs abstract away the HTTP request details and handle the inclusion of your Application ID and API Key. The following example demonstrates a search request using the JavaScript client:
const algoliasearch = require('algoliasearch');
// Initialize Algolia client with your Application ID and Search-Only API Key
const client = algoliasearch('YOUR_APPLICATION_ID', 'YOUR_SEARCH_ONLY_API_KEY');
const index = client.initIndex('your_index_name');
// Perform a search query
index.search('query string', { hitsPerPage: 5 })
.then(({ hits }) => {
console.log(hits);
})
.catch(err => {
console.error(err);
});
In this JavaScript example:
YOUR_APPLICATION_IDis your unique identifier for your Algolia application.YOUR_SEARCH_ONLY_API_KEYis the API key withsearchpermissions. This key is safe to embed in client-side code.your_index_namerefers to the specific Algolia index you wish to query.
For server-side operations requiring more privileged keys (e.g., Admin API Key or Write API Key), the structure is similar, but the key used would grant the necessary permissions. It is crucial never to expose Admin or Write keys in client-side code. Refer to the Algolia API reference for specific endpoint details and other SDK examples.
Security best practices
Adhering to security best practices is essential when working with Algolia API keys to protect your data and prevent unauthorized access. Key recommendations include:
- Principle of Least Privilege: Always use the most restrictive API key possible for any given task. For client-side search, use only the Search-Only API Key. Never embed an Admin or Write API Key in front-end code.
- Secure Storage: Store Admin and Write API Keys securely on your server-side environment. Avoid hardcoding them directly into your application's source code. Instead, use environment variables, secret management services (e.g., AWS Secrets Manager, Google Secret Manager), or secure configuration files AWS Secrets Manager documentation.
- HTTPS Everywhere: All communication with Algolia is automatically encrypted via HTTPS/TLS. Ensure your applications always use HTTPS to connect to Algolia endpoints.
- Scoped API Keys: Utilize Algolia's feature to create custom API keys with specific Access Control Lists (ACLs), validities, and rate limits. This allows you to restrict a key's permissions to certain indices, operations, or timeframes, minimizing the blast radius if a key is compromised.
- Key Rotation: Regularly rotate your API keys, especially those with high privileges (Admin and Write keys). This practice limits the window of opportunity for an attacker if a key is compromised without your knowledge. Algolia provides functionality to regenerate keys in the dashboard.
- Monitor API Usage: Regularly review your Algolia usage analytics and logs for any unusual activity. Spikes in requests or unexpected operations could indicate a compromised key or malicious activity.
- Server-Side Indexing: All indexing operations (adding, updating, deleting records) should originate from your secure back-end servers, never directly from client-side applications. Your back-end can authenticate with an Admin or Write API Key, and then expose a controlled API for your front-end.
- Front-End Search Limitations: While the Search-Only API Key is safe for client-side use, consider implementing additional server-side validation or proxying search requests if you need to enforce complex business logic or integrate with other systems before displaying results.
- Strict Network Policies: If possible, restrict network access to your Algolia API keys from specific IP addresses using firewall rules or VPC configurations, adding another layer of security.