A JavaScript SDK for interacting with the MemberPress REST API. This SDK provides a simple and intuitive interface for accessing MemberPress functionality from Node.js and browser applications.
With the MemberPress SDK, you can manage members, access membership products, process transactions, and handle subscriptions programmatically through a clean, consistent API.
npm install memberpress-sdk
<script src="path/to/memberpress-sdk.js"></script>
// Import the SDK
const { MemberPressSDK } = require('memberpress-sdk');
// Initialize the SDK with your MemberPress API key
const sdk = new MemberPressSDK({
apiKey: 'your-memberpress-api-key', // Found in MemberPress admin area
baseUrl: 'https://yoursite.com/wp-json/mp/v1',
debug: true // Optional
});
// Use the SDK to interact with MemberPress
async function getMemberships() {
try {
const memberships = await sdk.memberships.list();
console.log('Available memberships:', memberships);
} catch (error) {
console.error('Error fetching memberships:', error);
}
}
getMemberships();
- Log in to your WordPress admin dashboard
- Navigate to MemberPress → Settings → Developer Tools
- Look for the "API Keys" section
- You may need to generate a new API key if one doesn't exist
- Copy the API key to use with the SDK
For the MemberPress REST API to function correctly:
-
Your WordPress site must have permalinks set to anything other than "Plain"
- Go to Settings → Permalinks
- Select any option other than "Plain" (usually "Post name" is recommended)
- Save Changes
-
The MemberPress REST API must be enabled
- Go to MemberPress → Settings → Developer Tools
- Ensure the REST API option is enabled
- Members Management: Create, retrieve, and update members
- Membership Access: List memberships and check access rights
- Transaction Handling: View transaction history and details
- Subscription Management: List, retrieve, and cancel subscriptions
- Automatic Caching: Responses are cached to improve performance
- Comprehensive Error Handling: Detailed error information
- Debug Mode: Optional logging for troubleshooting
const sdk = new MemberPressSDK({
apiKey: 'your-memberpress-api-key',
baseUrl: 'https://yoursite.com/wp-json/mp/v1',
debug: false, // Optional, defaults to false
cacheTimeout: 300000 // Optional, defaults to 5 minutes (300000ms)
});
// Create a new member
const newMember = await sdk.members.create({
email: 'user@example.com',
firstName: 'John',
lastName: 'Doe'
});
// Get a member by ID
const member = await sdk.members.get(memberId);
// Update a member's status
const updatedMember = await sdk.members.updateStatus(memberId, 'active');
// List members with optional filters
const members = await sdk.members.list({
limit: 10,
offset: 0,
status: 'active'
});
// Check if a member has access to a specific membership
const access = await sdk.members.checkAccess(memberId, membershipId);
// List all memberships
const memberships = await sdk.memberships.list();
// Get a membership by ID
const membership = await sdk.memberships.get(membershipId);
// List transactions with optional filters
const transactions = await sdk.transactions.list({
limit: 10,
status: 'complete'
});
// Get a transaction by ID
const transaction = await sdk.transactions.get(transactionId);
// List subscriptions with optional filters
const subscriptions = await sdk.subscriptions.list({
member_id: memberId,
status: 'active'
});
// Get a subscription by ID
const subscription = await sdk.subscriptions.get(subscriptionId);
// Cancel a subscription
const result = await sdk.subscriptions.cancel(subscriptionId);
The SDK throws errors for API failures. We recommend using try/catch blocks:
try {
const result = await sdk.members.get(memberId);
// Process successful result
} catch (error) {
if (error.message.includes('401')) {
console.error('Authentication failed. Check your API key.');
} else if (error.message.includes('404')) {
console.error('Resource not found. Check the ID.');
} else {
console.error('Unexpected error:', error);
}
}
The SDK works in modern browsers and Node.js environments. For older browsers, you may need to use a polyfill for fetch.
For complete documentation, visit:
If you encounter any issues or have questions, please:
- Check the Troubleshooting Guide
- Search for similar issues in the GitHub Issues
- Open a new issue if your problem persists
Contributions are welcome! Please feel free to submit a Pull Request.
MIT