Authentication
The Market Data PHP SDK requires an API token to authenticate requests. You can obtain a token from the Market Data Dashboard.
Token Configuration
The SDK supports multiple methods for providing your API token, with the following priority order (highest to lowest):
- Explicit token parameter - Passed directly to the Client constructor
- Environment variable -
MARKETDATA_TOKEN - .env file - Using phpdotenv
- Empty string - For accessing free endpoints only
Setting the Environment Variable
- Mac/Linux
- Windows
Add to your shell profile (.bashrc, .zshrc, etc.):
export MARKETDATA_TOKEN="your_api_token_here"
Then reload your shell:
source ~/.bashrc # or source ~/.zshrc
Using Command Prompt:
setx MARKETDATA_TOKEN "your_api_token_here"
Using PowerShell:
[Environment]::SetEnvironmentVariable("MARKETDATA_TOKEN", "your_api_token_here", "User")
Using a .env File
Create a .env file in your project root:
MARKETDATA_TOKEN=your_api_token_here
The SDK automatically loads .env files using phpdotenv.
Explicit Token Parameter
Pass the token directly to the Client constructor:
<?php
use MarketDataApp\Client;
$client = new Client('your_api_token_here');
Token Validation
The SDK validates your token during client initialization by making a request to the /user/ endpoint. If the token is invalid, an UnauthorizedException is thrown:
<?php
use MarketDataApp\Client;
use MarketDataApp\Exceptions\UnauthorizedException;
try {
$client = new Client('invalid_token');
} catch (UnauthorizedException $e) {
echo "Invalid API token: " . $e->getMessage();
}
Testing Your Token
Verify your token is working correctly:
<?php
require_once 'vendor/autoload.php';
use MarketDataApp\Client;
$client = new Client();
// Get rate limit information
$user = $client->utilities->user();
echo "Requests Used Today: " . $user->requests_made_today . "\n";
echo "Daily Request Limit: " . $user->daily_requests . "\n";
echo "Requests Remaining: " . ($user->daily_requests - $user->requests_made_today) . "\n";
Security Best Practices
- Never commit your API token to version control
- Add
.envto your.gitignorefile - Use environment variables in production environments
- Rotate tokens regularly if compromised
Next Steps
After configuring authentication, learn about the Client class to start making API requests.