> ## Documentation Index
> Fetch the complete documentation index at: https://docs.browsertest.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Authentication

> Learn how to authenticate with BrowserTest API

## API Key Authentication

BrowserTest SDK uses API key authentication for all requests. You need a valid API key to use any SDK features.

### Getting Your API Key

1. Visit [BrowserTest Dashboard](https://browsertest.in)
2. Sign up or log in to your account
3. Navigate to the API Keys section
4. Generate a new API key

<Callout type="warning">
  Keep your API key secure! Never commit it to version control or share it publicly.
</Callout>

### Initializing the SDK

```javascript theme={null}
import { BrowserTest } from 'browsertest-sdk';

const bt = new BrowserTest({
  apiKey: 'your-api-key-here'
});
```

### Environment Variables

Always use environment variables for API keys:

```javascript theme={null}
// .env file
BROWSERTEST_API_KEY=your-api-key-here
```

```javascript theme={null}
// In your code
const bt = new BrowserTest({
  apiKey: process.env.BROWSERTEST_API_KEY
});
```

### Configuration Options

```javascript theme={null}
const bt = new BrowserTest({
  apiKey: process.env.BROWSERTEST_API_KEY,
  baseUrl: 'https://api.browsertest.in', // Optional: defaults to production API
  timeout: 30000, // Request timeout in milliseconds
  retries: 3 // Number of retries for failed requests
});
```

### Testing Authentication

```javascript theme={null}
// Test if your API key is valid
const isConnected = await bt.testConnection();
console.log('Authentication:', isConnected ? '✅ Valid' : '❌ Invalid');
```

### Error Handling

The SDK throws specific errors for authentication issues:

```javascript theme={null}
try {
  const result = await bt.screenshot.take({ url: 'https://example.com' });
} catch (error) {
  if (error.name === 'BrowserTestAuthError') {
    console.log('Invalid API key or authentication failed');
  } else if (error.name === 'BrowserTestQuotaError') {
    console.log('Quota exceeded - check your plan limits');
  }
}
```

### Rate Limits & Quotas

Each API key has associated quotas based on your plan:

* **Starter**: 1,000 screenshots/month, 100 agentic tests/month
* **Pro**: 10,000 screenshots/month, 500 agentic tests/month
* **Enterprise**: Custom limits

### Checking Usage

```javascript theme={null}
const usage = await bt.getUsage();
console.log('Current plan:', usage.plan);
console.log('Screenshots used:', usage.usage.screenshot.used);
console.log('Screenshots remaining:', usage.usage.screenshot.remaining);
console.log('Agentic tests used:', usage.usage.agentic.used);
console.log('Agentic tests remaining:', usage.usage.agentic.remaining);
```

### API Key Security Best Practices

1. **Never commit API keys** to version control
2. **Use environment variables** in production
3. **Rotate keys regularly** for security
4. **Use different keys** for different environments (dev/staging/prod)
5. **Monitor usage** to detect unauthorized access

### Multiple Environments

```javascript theme={null}
// Different keys for different environments
const config = {
  development: process.env.BROWSERTEST_DEV_KEY,
  staging: process.env.BROWSERTEST_STAGING_KEY,
  production: process.env.BROWSERTEST_PROD_KEY
};

const bt = new BrowserTest({
  apiKey: config[process.env.NODE_ENV || 'development']
});
```
