SDK Reference
Authentication
Learn how to authenticate with the SweatHost API using API keys
Authentication
The SweatHost SDK uses API keys for authentication. You can create and manage API keys from your dashboard.
Creating an API Key
- Log in to your SweatHost Dashboard
- Navigate to API Keys in the sidebar
- Click Create API Key
- Give your key a name and select the appropriate scopes
- Copy the API key (it will only be shown once!)
Initializing the SDK
import { SweatHostClient } from '@sweathost/sdk';
const client = new SweatHostClient({
apiKey: 'your-api-key-here',
baseUrl: 'https://api.sweathost.com', // optional, defaults to production
});Configuration Options
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
apiKey | string | Yes | - | Your API key from the dashboard |
baseUrl | string | No | https://api.sweathost.com | API base URL |
timeout | number | No | 30000 | Request timeout in milliseconds |
Example: Complete Setup
import { SweatHostClient } from '@sweathost/sdk';
// Initialize the client
const client = new SweatHostClient({
apiKey: process.env.SWEATHOST_API_KEY!,
baseUrl: process.env.SWEATHOST_API_URL || 'https://api.sweathost.com',
timeout: 60000, // 60 seconds
});
// Test the connection
async function testConnection() {
try {
const servers = await client.gameServers.list({ page: 1, pageSize: 10 });
console.log(`✓ Connected! Found ${servers.total} servers`);
} catch (error) {
console.error('✗ Connection failed:', error.message);
}
}
testConnection();API Key Scopes
API keys can have different scopes to limit their permissions:
| Scope | Description |
|---|---|
servers:read | Read game server information |
servers:write | Create, update, and delete game servers |
servers:control | Start, stop, and restart servers |
matches:read | Read match data and statistics |
matches:write | Create and update matches |
regions:read | Read region information |
Security Best Practices
Never expose your API key in client-side code or public repositories!
- Store keys securely: Use environment variables or a secrets manager
- Use minimal scopes: Only grant the permissions your application needs
- Rotate keys regularly: Create new keys and delete old ones periodically
- Monitor usage: Check your API key usage in the dashboard
- Use different keys: Use separate keys for development, staging, and production
Error Handling
The SDK will throw an ApiClientError if authentication fails:
import { SweatHostClient, ApiClientError } from '@sweathost/sdk';
const client = new SweatHostClient({
apiKey: 'invalid-key',
});
try {
await client.gameServers.list();
} catch (error) {
if (error instanceof ApiClientError) {
if (error.statusCode === 401) {
console.error('Invalid API key');
} else if (error.statusCode === 403) {
console.error('Insufficient permissions');
}
}
}Next Steps
- Game Servers API - Manage game servers
- Matches API - Access match data
- Regions API - Query available regions