An unofficial TypeScript SDK for interacting with the WP Engine API. This SDK provides a simple and type-safe way to interact with WP Engine's services from both Node.js and browser environments.
Note: This is an unofficial SDK maintained by Jeremy Pollock (jeremy.pollock@wpengine.com) and is not affiliated with or supported by WP Engine.
- Full TypeScript support with complete type definitions
- Environment-based configuration
- Support for all WP Engine API endpoints
- Built-in authentication handling
- Comprehensive input validation
- Client-side rate limiting
- Secure error handling
- Example implementations
- Unit and functional tests
- Support for both CommonJS and ES Modules
npm install @elasticapi/wpengine-typescript-sdk
There are several ways to initialize the SDK:
import { WPEngineSDK } from '@elasticapi/wpengine-typescript-sdk';
const sdk = new WPEngineSDK({
username: 'your-api-username',
password: 'your-api-password'
}, undefined, 'Default', {
maxRequestsPerSecond: 5 // Optional: Default is 5
});
Create a .env
file:
WPENGINE_USERNAME=your-api-username
WPENGINE_PASSWORD=your-api-password
Then initialize:
const sdk = new WPEngineSDK();
Create a configuration file (e.g., config.ini
):
[Default]
WPENGINE_USERNAME=your-api-username
WPENGINE_PASSWORD=your-api-password
[Production]
WPENGINE_USERNAME=prod-api-username
WPENGINE_PASSWORD=prod-api-password
Then initialize with a specific profile:
const sdk = new WPEngineSDK(undefined, './config.ini', 'Production');
The SDK includes built-in rate limiting to prevent API throttling:
import { WPEngineSDK, RateLimitError } from '@elasticapi/wpengine-typescript-sdk';
// Initialize with custom rate limit
const sdk = new WPEngineSDK(credentials, undefined, 'Default', {
maxRequestsPerSecond: 5 // Limit to 5 requests per second
});
// Handle rate limit errors
try {
await sdk.accounts.listAccounts();
} catch (error) {
if (error instanceof RateLimitError) {
console.log('Rate limit exceeded, waiting...');
}
}
// Check rate limiter status
const stats = sdk.getRateLimiterStats();
console.log('Available requests:', stats.availableTokens);
console.log('Wait time (ms):', stats.waitTime);
The rate limiter uses a token bucket algorithm to:
- Allow burst traffic up to the limit
- Smoothly handle sustained traffic
- Automatically queue requests when limit is reached
- Provide visibility into rate limit status
The SDK includes comprehensive input validation to prevent errors and improve security:
import { WPEngineSDK, ValidationError } from '@elasticapi/wpengine-typescript-sdk';
try {
await sdk.accountUsers.createAccountUser('account-id', {
user: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
roles: 'full'
}
});
} catch (error) {
if (error instanceof ValidationError) {
console.log('Validation failed:', error.message);
}
}
See Validation Documentation for detailed information about the validation system.
-
Rate Limiting
- Use appropriate rate limits for your use case
- Handle rate limit errors gracefully
- Monitor rate limit statistics
-
Credential Storage
- Never commit credentials to version control
- Use environment variables in production environments
- Ensure configuration files have appropriate permissions (0600)
-
Input Validation
- Always handle validation errors appropriately
- Validate user input before making API calls
- Use TypeScript types for additional type safety
// List users in an account
const users = await sdk.accountUsers.listAccountUsers('account-id');
// Add a user to an account
await sdk.accountUsers.createAccountUser('account-id', {
user: {
account_id: 'account-id',
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
roles: 'full'
}
});
// Remove a user from an account
await sdk.accountUsers.deleteAccountUser('account-id', 'user-id');
// List sites
const sites = await sdk.sites.listSites();
// Get site details
const site = await sdk.sites.getSite('site-id');
// List backups
const backups = await sdk.backups.listBackups('install-id');
// Create a backup
await sdk.backups.createBackup('install-id', {
backup: {
description: 'Pre-deployment backup'
}
});
The SDK uses standard Promise-based error handling with additional error types:
try {
const users = await sdk.accountUsers.listAccountUsers('account-id');
} catch (error) {
if (error instanceof ValidationError) {
// Handle validation error
console.error('Validation Error:', error.message);
} else if (error instanceof RateLimitError) {
// Handle rate limit error
console.error('Rate Limit Error:', error.message);
} else if (error.response) {
// API error response
console.error('API Error:', error.response.data);
} else if (error.request) {
// Network error
console.error('Network Error:', error.message);
} else {
// Other error
console.error('Error:', error.message);
}
}
npm run build
npm test
npm run example:user
npm run example:site
npm run example:backup
npm run example:validation
npm run example:rate-limit
MIT
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a new Pull Request
This SDK is maintained by Jeremy Pollock (jeremy.pollock@wpengine.com). For any questions, issues, or contributions, please reach out directly.