4.3 KiB
4.3 KiB
Pennyzo API Service
A comprehensive TypeScript class for handling all API interactions with the Pennyzo General API.
Features
- Complete JWT authentication lifecycle management
- Automatic token handling for authenticated requests using Axios interceptors
- Comprehensive error handling
- TypeScript interfaces for request bodies and response payloads
- Singleton pattern for easy usage throughout the application
Installation
The service is already part of the Pennyzo Web project. It requires Axios, which is installed as a project dependency.
API Methods
System & Registration (Public)
// Check health status of the API
const healthStatus = await pennyzoApi.checkHealth();
// Register a new user
const registeredUser = await pennyzoApi.registerUser({
first_name: "John",
last_name: "Doe",
email: "john.doe@example.com",
// other required fields...
});
Authentication & Verification (Public)
// Login a user
const loginResult = await pennyzoApi.login("user@example.com", "password123");
// Verify email with OTP
const verificationResult = await pennyzoApi.verifyEmail(
"user@example.com",
"123456"
);
// Resend verification OTP
const resendResult = await pennyzoApi.resendVerificationOtp("user@example.com");
Password Management (Public/Authenticated)
// Request password reset
const forgotPasswordResult = await pennyzoApi.forgotPassword(
"user@example.com"
);
// Reset password with OTP
const resetResult = await pennyzoApi.resetPasswordWithOtp(
"user@example.com",
"123456",
"newPassword123"
);
// Change password (authenticated)
const changePasswordResult = await pennyzoApi.changePassword(
"currentPassword123",
"newPassword123"
);
User & Profile Management (Authenticated)
// Get current user information
const currentUser = await pennyzoApi.getCurrentUser();
// Get user profile
const userProfile = await pennyzoApi.getUserProfile();
// Update user profile
const updatedProfile = await pennyzoApi.updateUserProfile({
first_name: "Updated",
last_name: "Name",
// other fields to update
});
// Update company information
const updatedCompany = await pennyzoApi.updateCompanyInfo({
company_name: "New Company Name",
industry: "Technology",
});
// Update GST registration
const updatedGst = await pennyzoApi.updateGstRegistration({
gst_registered: true,
gst_number: "GSTIN12345678",
});
Membership & Stats (Authenticated)
// Upgrade membership
const upgradedMembership = await pennyzoApi.upgradeMembership({
plan_type: "ELITE",
membership_type: "PAID",
billing_cycle: "yearly",
});
// Get membership information
const membershipInfo = await pennyzoApi.getMembershipInfo();
// Get user statistics
const userStats = await pennyzoApi.getUserStats();
Session Management
// Logout user (clears stored token)
pennyzoApi.logout();
// Check if user is authenticated
const isAuthenticated = pennyzoApi.isAuthenticated();
// Get the current access token (if any)
const accessToken = pennyzoApi.getAccessToken();
Error Handling
The service handles errors gracefully and provides meaningful error objects with details:
try {
const profile = await pennyzoApi.getUserProfile();
} catch (error) {
console.error("Error status:", error.status);
console.error("Error message:", error.message);
console.error("Error code:", error.code);
console.error("Error details:", error.details);
}
Implementation Details
- Uses Axios for all HTTP requests
- Base URL is set to
https://api.pennyzo.com/api/v1/general - Automatically attaches Bearer token to authenticated requests
- Handles token storage in localStorage
- Special handling for form-urlencoded content type (login endpoint)
- Automatic 401 error handling to clear invalid tokens
Usage Example
// Import the singleton instance
import { pennyzoApi } from "../services";
// Use in async/await context
async function fetchUserData() {
try {
// First authenticate
await pennyzoApi.login("user@example.com", "password");
// Then make authenticated calls
const profile = await pennyzoApi.getUserProfile();
const stats = await pennyzoApi.getUserStats();
return { profile, stats };
} catch (error) {
console.error("API Error:", error.message);
throw error;
}
}