Authentication overview
IEX Cloud provides access to its financial data APIs through a credential-based authentication system. This system ensures that only authorized applications can retrieve data, protecting both the API infrastructure and user accounts. The core of IEX Cloud's authentication mechanism revolves around the use of API keys, which are unique identifiers assigned to each user account. These keys are used to verify the identity of the client making a request and to determine their access privileges based on their subscription plan.
The authentication process typically involves including a specific API key with each request to the IEX Cloud API. This key acts as a digital signature, allowing the IEX Cloud servers to confirm the request's origin and validity. Without a valid API key, requests are generally rejected, preventing unauthorized access to market data, company fundamentals, and other financial information available through the platform. Adhering to secure authentication practices is essential for maintaining the integrity and confidentiality of data interactions with IEX Cloud.
Supported authentication methods
IEX Cloud primarily utilizes API keys for authenticating requests. This method is common across many web APIs due to its simplicity and effectiveness in managing access. API keys for IEX Cloud are divided into two types:
- Publishable Token: Used for making requests from client-side applications or environments where the key might be exposed (e.g., frontend JavaScript). It grants access to public data endpoints and is generally considered less sensitive.
- Secret Token: Used for making requests from server-side applications or secure backend environments. This token provides access to all data endpoints, including more sensitive ones, and must be kept confidential. Its exposure could lead to unauthorized usage of your account's message quota or access to restricted data.
The choice between using a publishable or secret token depends on the security context of your application. For applications where the API key is embedded in client-side code, the publishable token is appropriate. For server-to-server communication or backend processing, the secret token should be used and protected diligently.
The following table summarizes the authentication methods supported by IEX Cloud:
| Method | When to Use | Security Level |
|---|---|---|
| API Key (Publishable Token) | Client-side applications, public data access | Moderate (intended for public exposure) |
| API Key (Secret Token) | Server-side applications, all data access | High (requires strict confidentiality) |
Getting your credentials
To obtain your IEX Cloud API keys, you must first register for an account on the IEX Cloud platform. Once registered, you can access your API tokens through the IEX Cloud Console. The process typically involves these steps:
- Sign Up/Log In: Navigate to the IEX Cloud homepage and either create a new account or log in to an existing one.
- Access Dashboard: After logging in, you will be directed to your account dashboard or console.
- Locate API Tokens: Within the console, there is usually a section dedicated to API tokens or keys. This section will display both your publishable and secret tokens for both the sandbox (testing) and production (live data) environments. For specific instructions, refer to the IEX Cloud documentation on API tokens.
- Copy and Store: Carefully copy your API keys. It is recommended to store them securely, preferably using environment variables or a secure configuration management system, rather than hardcoding them directly into your application code.
IEX Cloud provides separate keys for a sandbox environment, which allows developers to test their applications with simulated data without incurring message usage. Once development and testing are complete, you can switch to your production keys to access live market data.
Authenticated request example
Authenticating requests to the IEX Cloud API involves appending your API key to the request URL as a query parameter. The parameter name is typically token. Below are examples demonstrating how to make an authenticated request using cURL, Python, and JavaScript.
cURL Example
This cURL command retrieves the latest price for Apple (AAPL) using a publishable token. Replace YOUR_PUBLISHABLE_TOKEN with your actual publishable key.
curl 'https://cloud.iexapis.com/stable/stock/aapl/quote?token=YOUR_PUBLISHABLE_TOKEN'
Python Example
Using the requests library in Python, you can make an authenticated call. It's recommended to load your token from an environment variable for security.
import os
import requests
# Load your publishable token from an environment variable
# Example: export IEX_PUBLISHABLE_TOKEN="pk_YOUR_PUBLISHABLE_TOKEN"
publishable_token = os.getenv("IEX_PUBLISHABLE_TOKEN")
if not publishable_token:
print("Error: IEX_PUBLISHABLE_TOKEN environment variable not set.")
exit()
symbol = "AAPL"
url = f"https://cloud.iexapis.com/stable/stock/{symbol}/quote"
params = {"token": publishable_token}
try:
response = requests.get(url, params=params)
response.raise_for_status() # Raise an exception for HTTP errors
data = response.json()
print(data)
except requests.exceptions.HTTPError as err:
print(f"HTTP error occurred: {err}")
except Exception as err:
print(f"An error occurred: {err}")
JavaScript Example (Node.js using fetch)
For Node.js environments, you can use the built-in fetch API or a library like axios. Again, environment variables are preferred for token storage.
// In your terminal before running: export IEX_PUBLISHABLE_TOKEN="pk_YOUR_PUBLISHABLE_TOKEN"
const publishableToken = process.env.IEX_PUBLISHABLE_TOKEN;
if (!publishableToken) {
console.error("Error: IEX_PUBLISHABLE_TOKEN environment variable not set.");
process.exit(1);
}
const symbol = "AAPL";
const url = `https://cloud.iexapis.com/stable/stock/${symbol}/quote?token=${publishableToken}`;
async function getStockQuote() {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error("An error occurred:", error);
}
}
getStockQuote();
Security best practices
Properly securing your API keys is paramount to prevent unauthorized access to your IEX Cloud account and to manage your message usage effectively. Adhering to these best practices can mitigate common security risks:
- Never Hardcode API Keys: Avoid embedding API keys directly into your source code. Hardcoding makes keys vulnerable if the code repository is compromised or publicly exposed.
- Use Environment Variables: Store API keys as environment variables on your server or development machine. This isolates sensitive information from your codebase and allows for easier rotation of keys. This practice is widely recommended for securing credentials, as detailed in guides like Google Cloud's API key security documentation.
- Protect Secret Tokens: Your Secret Token should only be used in secure, server-side environments. Never expose it in client-side code (e.g., frontend JavaScript, mobile apps) or public repositories.
- Restrict API Key Privileges: While IEX Cloud's keys are generally tied to your account, understand the distinction between publishable and secret tokens. Use the least privileged key necessary for any given task.
- Implement IP Whitelisting (if available): If IEX Cloud offers IP whitelisting, configure it to allow API requests only from known and trusted IP addresses. This adds an extra layer of security by restricting where requests can originate.
- Monitor API Usage: Regularly check your IEX Cloud dashboard for unusual activity or spikes in API usage. This can help detect unauthorized use of your API keys.
- Key Rotation: Periodically rotate your API keys. If a key is compromised, rotating it minimizes the window of vulnerability. IEX Cloud typically allows you to generate new keys from your console.
- Secure Your Development Environment: Ensure that your development machines and deployment environments are secure. Use strong passwords, enable multi-factor authentication, and keep software updated to prevent malware or unauthorized access that could expose your keys.
- HTTPS Only: Always use HTTPS for all API requests to IEX Cloud. HTTPS encrypts the communication channel, protecting your API key and data from eavesdropping during transit. The IETF's HTTP/1.1 specification highlights the importance of transport-layer security for sensitive data.
By implementing these security measures, developers can significantly reduce the risk of API key compromise and ensure the secure operation of applications relying on IEX Cloud data.