Authentication overview

Countly's authentication mechanisms are designed to secure access to its analytics platform for both data collection and administrative tasks. The primary method for authenticating requests to Countly involves the use of API keys. These keys serve as credentials to identify and authorize applications or users when interacting with Countly's various endpoints, including those for event tracking, user profile management, and data querying. The specific type of API key required depends on the operation being performed and the endpoint being accessed.

Countly differentiates between several types of API keys to enforce granular access control:

  • App Keys: Used by client-side SDKs (e.g., mobile, web) to submit analytics data to a specific application within Countly. These keys are generally considered less sensitive as they grant write-only access to event data.
  • API Keys (Management): Provide access to administrative and data retrieval endpoints. These keys can be used for tasks such as querying reports, managing applications, or exporting data. They typically carry higher privileges and require stricter security measures.
  • User Keys: Associated with specific Countly user accounts for accessing the web interface and potentially for API interactions depending on configuration.

All communication with Countly servers, regardless of the API key type, is expected to occur over HTTPS (TLS/SSL) to encrypt data in transit and protect credentials from eavesdropping. This is a standard security practice for web APIs to prevent unauthorized interception of sensitive information, as detailed in web security guides like Cloudflare's explanation of SSL/TLS.

Supported authentication methods

Countly primarily utilizes API keys for authentication across its platform. While core SDKs mostly use App Keys for data submission, the management APIs and specific server-side integrations may require different API Key types with broader permissions. The table below outlines the primary authentication methods used within the Countly ecosystem:

Method When to Use Security Level
App Key Client-side SDKs (mobile, web) for sending event data, user profiles, and crash reports. Moderate (write-only access to specific app, generally exposed in client code)
API Key (Management) Server-side integrations, custom dashboards, data export, administrative tasks, querying aggregated data. High (read/write access to broader data/settings, must be kept secret)
User Authentication (Login) Accessing the Countly web interface for dashboard viewing, configuration, and administration. High (session-based, tied to user roles and permissions)

Countly's API reference documentation provides detailed information on which key types are required for specific API endpoints and operations, ensuring developers can implement the correct authentication strategy for their integrations. For example, the Countly API Reference v1.1 specifies the use of an app_key parameter for recording events and a separate api_key for management calls.

Getting your credentials

Obtaining the necessary API keys for Countly involves interacting with the Countly dashboard. The process generally follows these steps, though specifics may vary slightly between self-hosted Community Edition and cloud/Enterprise versions:

  1. Log in to your Countly Dashboard: Access your Countly instance using your administrator or developer credentials.
  2. Select/Create an Application: API keys are scoped to specific applications within Countly. If you don't have an application setup, you will need to create one first. Navigate to Management > Applications and either select an existing app or create a new one.
  3. Locate App Key: For client-side data collection, the App Key is typically displayed prominently on the application's main configuration page within the dashboard. This key is passed to the SDKs during initialization.
  4. Generate API Keys (Management): For server-side integrations and management API access, you will usually find options to generate or manage API keys under sections like Management > API Keys or within specific application settings under an 'API' or 'Developers' tab. You might be able to define scopes or permissions for these keys, granting them specific read/write access to certain data or administrative functions, as described in the Countly API documentation.

It is crucial to note that management API keys, especially those with broad permissions, should be treated with the same level of security as passwords. They should not be hardcoded directly into client-side applications or publicly accessible repositories. For production environments, consider using environment variables, secret management services, or secure configuration files to store and retrieve these keys.

Authenticated request example

Interacting with Countly's APIs requires including the appropriate API key in your requests. The method for inclusion varies based on whether it's an App Key for data collection or an API Key for management operations. All requests should be made over HTTPS.

Example 1: Sending an event with an App Key (Client-side)

When using a Countly SDK, the App Key is typically configured during initialization. The SDK then automatically includes it with all subsequent requests. Here's a conceptual example using the JavaScript SDK:


// Initialize Countly with your App Key and server URL
Countly.init({
    app_key: '<YOUR_APP_KEY>',
    url: 'https://your.countly.server',
    app_version: '1.0',
    debug: true
});

// Track a custom event
Countly.track_event({
    key: 'button_click',
    segmentation: {
        button_name: 'Submit',
        page: 'Homepage'
    }
});

In this example, the app_key is passed once during initialization. The SDK handles the inclusion of this key in the payload sent to the Countly server for each tracked event.

Example 2: Querying data with a Management API Key (Server-side)

For management API calls, the API Key (Management) is typically sent as a query parameter or sometimes a header, as specified by the API endpoint. The Countly API documentation details the required parameters.

Here's a conceptual Python example demonstrating a request to retrieve aggregated data:


import requests
import json

COUNTLY_SERVER_URL = 'https://your.countly.server'
MANAGEMENT_API_KEY = '<YOUR_MANAGEMENT_API_KEY>'
APP_ID = '<YOUR_COUNTLY_APP_ID>' # Often required for management calls

# Endpoint to fetch overall application usage stats
endpoint = f"{COUNTLY_SERVER_URL}/o/analytics/app_usage"

params = {
    'api_key': MANAGEMENT_API_KEY,
    'app_id': APP_ID,
    'period': 'daily',
    'date': '2026-05-28' # Example date
}

try:
    response = requests.get(endpoint, params=params)
    response.raise_for_status() # Raise an exception for HTTP errors
    data = response.json()
    print(json.dumps(data, indent=2))

except requests.exceptions.RequestException as e:
    print(f"API request failed: {e}")
except json.JSONDecodeError:
    print("Failed to decode JSON response.")

In this server-side example, both the api_key and app_id are passed as query parameters within the URL. It's important to replace placeholders with your actual credentials and endpoint details.

Security best practices

Securing your Countly instance and the data flowing through it requires adhering to established security practices for API keys and data handling. Given Countly's focus on privacy and self-hosting options, these practices are particularly relevant:

  • Keep Management API Keys Confidential: Treat management API keys as sensitive as passwords. Never hardcode them directly into client-side code, commit them to public version control systems (like GitHub without proper protection), or expose them in publicly accessible areas.
  • Use Environment Variables or Secret Management: For server-side applications, store API keys in environment variables or use dedicated secret management services (e.g., AWS Secrets Manager, Google Secret Manager, Azure Key Vault, or a similar solution for self-hosted instances) to inject them securely at runtime. This practice reduces the risk of accidental exposure.
  • Scope API Key Permissions: If Countly allows for granular key permissions (e.g., read-only vs. read/write, access to specific reports), generate keys with the minimum necessary privileges. A key used only for reading analytics data should not have permissions to modify application settings.
  • Rotate API Keys Regularly: Implement a strategy to periodically rotate your API keys. This practice limits the window of exposure if a key is compromised.
  • Monitor API Key Usage: Keep an eye on API access logs provided by Countly (especially in self-hosted deployments) for unusual activity that might indicate a compromised key or unauthorized access attempts.
  • Use HTTPS Everywhere: Ensure all communication with your Countly server (both SDKs and API calls) uses HTTPS. This encrypts data in transit, protecting both the analytics data and your API keys from interception. Many resources, including Mozilla's guide on secure contexts, emphasize the importance of HTTPS.
  • Secure Your Countly Server (Self-Hosted): If you are running a self-hosted Countly instance, apply general server security best practices. This includes regular patching, strong firewall rules, restricting access to the server, and using strong authentication for server access.
  • Never Expose App Keys for Sensitive Operations: While App Keys are less sensitive than Management API Keys, they should still be handled with care. Do not use App Keys for operations beyond basic data collection. If an SDK requires a more privileged key for specific features, ensure that key is not exposed or that its permissions are tightly scoped.