@oxheberg/whmcs-api
Version:
A fully featured and type-safe TypeScript client for the WHMCS API. Includes all official endpoints, modular design, helpful errors, and built with modern developer experience in mind.
285 lines (219 loc) โข 7.36 kB
Markdown
# /whmcs-api
[](https://badge.fury.io/js/@oxheberg%2Fwhmcs-api)
[](https://opensource.org/licenses/MIT)
[](https://www.typescriptlang.org/)
A comprehensive, type-safe TypeScript wrapper for the WHMCS API. Built with modern TypeScript practices, this library provides a clean, intuitive interface to interact with all WHMCS API endpoints.
## โจ Features
- ๐ท **Full TypeScript Support** - Complete type definitions for all API methods and responses
- ๐ **164 API Methods** - Comprehensive coverage of WHMCS API endpoints
- ๐ฏ **Optimized Responses** - Clean, deduplicated data structures for better DX
- ๐ก๏ธ **Error Handling** - Built-in error handling with detailed error types
- ๐ฆ **Modular Architecture** - Organized by functional modules (Client, Billing, Support, etc.)
- ๐ **Universal** - Works in Node.js, serverless environments, and modern browsers
- ๐งช **Production Ready** - Battle-tested and actively maintained
## ๐ฆ Installation
```bash
npm install /whmcs-api
```
```bash
pnpm add /whmcs-api
```
```bash
yarn add /whmcs-api
```
## ๐ Quick Start
```typescript
import { WhmcsClient } from "@oxheberg/whmcs-api";
// Initialize the client
const whmcs = new WhmcsClient({
url: "https://your-whmcs-domain.com/includes/api.php",
identifier: "your-api-identifier",
secret: "your-api-secret",
accesskey: "your-access-key",
});
// Get client details
const response = await whmcs.client.getClientsDetails({
clientid: 1,
stats: true,
});
console.log(`Client: ${response.client.fullname}`);
console.log(`Email: ${response.client.email}`);
console.log(`Total Revenue: ${response.stats.income}`);
```
## ๐ก Usage Examples
### Client Management
```typescript
// Create a new client
const newClient = await whmcs.client.addClient({
firstname: "John",
lastname: "Doe",
email: "john.doe@example.com",
address1: "123 Main Street",
city: "New York",
state: "NY",
postcode: "10001",
country: "US",
phonenumber: "+1-555-123-4567",
password2: "securepassword123",
});
console.log(`Created client with ID: ${newClient.clientid}`);
// Get optimized client details (cleaned response)
const clientDetails = await whmcs.client.getClientsDetails({
clientid: newClient.clientid,
stats: true,
});
// Access clean, optimized data structure
console.log("Client Info:", {
id: clientDetails.client.id,
name: clientDetails.client.fullname,
email: clientDetails.client.email,
phone: clientDetails.client.phonenumberformatted,
emailPreferences: clientDetails.client.email_preferences,
});
// Users associated with the client (fully typesafe)
clientDetails.users.forEach((user) => {
console.log(`User: ${user.name} (Owner: ${user.is_owner})`);
});
```
## ๐ก๏ธ Error Handling
The library provides comprehensive error handling with detailed error information:
### Using async/await syntax:
```typescript
import { WhmcsClient, WhmcsError } from "@oxheberg/whmcs-api";
try {
const whmcs = new WhmcsClient({
url: "https://your-whmcs.com/includes/api.php",
identifier: "your-identifier",
secret: "your-secret",
accesskey: "your-access-key",
});
const result = await whmcs.client.getClientsDetails({
clientid: 999999, // Non-existent client
});
} catch (error) {
if (error instanceof WhmcsApiError) {
console.error("WHMCS API Error:", {
message: error.message,
});
} else {
console.error("Network or other error:", error);
}
}
```
### Using .then/.catch syntax:
```typescript
import { WhmcsClient, WhmcsError } from "@oxheberg/whmcs-api";
const whmcs = new WhmcsClient({
url: "https://your-whmcs.com/includes/api.php",
identifier: "your-identifier",
secret: "your-secret",
accesskey: "your-access-key",
});
whmcs.client
.getClientsDetails({
clientid: 999999, // Non-existent client
})
.then((result) => {
console.log("Client details:", result);
})
.catch((error) => {
if (error instanceof WhmcsApiError) {
console.error("WHMCS API Error:", {
message: error.message,
});
} else {
console.error("Network or other error:", error);
}
});
```
## โ๏ธ Configuration
### Basic Configuration
```typescript
const whmcs = new WhmcsClient({
url: "https://your-whmcs-domain.com/includes/api.php",
identifier: "your-api-identifier",
secret: "your-api-secret",
});
```
### Advanced Configuration
```typescript
const whmcs = new WhmcsClient({
url: "https://your-whmcs-domain.com/includes/api.php",
identifier: "your-api-identifier",
secret: "your-api-secret",
accesskey: "your-access-key", // to bypass IP constraints
timeout: 10000, // 10 seconds timeout
});
```
### Environment Variables
For security, use environment variables:
```bash
WHMCS_URL=https://your-whmcs-domain.com/includes/api.php
WHMCS_IDENTIFIER=your-api-identifier
WHMCS_SECRET=your-api-secret
WHMCS_ACCESS_KEY=your-access-key # optional
```
```typescript
const whmcs = new WhmcsClient({
url: process.env.WHMCS_URL!,
identifier: process.env.WHMCS_IDENTIFIER!,
secret: process.env.WHMCS_SECRET!,
accesskey: process.env.WHMCS_ACCESS_KEY!, // optional
});
```
## ๐๏ธ Architecture
The library is organized into logical modules:
```typescript
whmcs.addons; // Addon management operations
whmcs.affiliates; // Affiliate management operations
whmcs.authentication; // Authentication operations
whmcs.billing; // Billing and invoice operations
whmcs.client; // Client management operations
whmcs.domains; // Domain management operations
whmcs.modules; // Module management operations
whmcs.orders; // Order processing operations
whmcs.products; // Product management operations
whmcs.projectManagement; // Project management operations
whmcs.servers; // Server management operations
whmcs.services; // Service management operations
whmcs.support; // Support ticket operations
whmcs.system; // System administration operations
whmcs.tickets; // Ticket management operations
whmcs.users; // User management operations
```
## ๐ง Development
### Prerequisites
- Node.js 18+
- TypeScript 5+
### Setup
```bash
git clone https://github.com/oxheberg/whmcs-api
cd whmcs-api
npm install
```
### Build
```bash
npm run build
```
### Testing
```bash
npm test
```
## ๐ API Reference
For detailed API documentation, visit the [WHMCS API Documentation](https://developers.whmcs.com/api-reference/).
## ๐ค Contributing
We welcome contributions!
### Development Workflow
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## ๐ Issues
Found a bug? Have a feature request? Please [open an issue](https://github.com/oxheberg/whmcs-api/issues).
## โญ Support
If this library helps you, please consider giving it a โญ on GitHub!
---
**Made with โค๏ธ by [Oxheberg](https://oxheberg.com)**