Skip to main content

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):

  1. Explicit token parameter - Passed directly to the Client constructor
  2. Environment variable - MARKETDATA_TOKEN
  3. .env file - Using phpdotenv
  4. Empty string - For accessing free endpoints only

Setting the Environment Variable

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 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 .env to your .gitignore file
  • 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.