SDKs overview
Software Development Kits (SDKs) and libraries for CPFHub are designed to streamline the integration of external applications with the CPFHub platform. These tools provide pre-built functions and methods that encapsulate the underlying RESTful API calls, authentication mechanisms, and data serialization required for interacting with CPFHub services. By using an SDK, developers can reduce the amount of boilerplate code needed, accelerate development cycles, and minimize potential errors associated with direct API consumption.
CPFHub offers official SDKs for popular programming languages such as Node.js, Python, and Java, catering to a broad developer base. These SDKs are maintained by CPFHub and are the recommended method for integrating with the platform, ensuring compatibility with the latest API versions and features. The developer experience is supported by comprehensive API documentation and a dedicated sandbox environment, enabling thorough testing before deployment to production environments, as noted in the CPFHub documentation.
Official SDKs by language
CPFHub provides official SDKs to simplify interaction with its APIs across different programming environments. These SDKs handle common tasks such as request signing, error handling, and data parsing, allowing developers to focus on application logic rather than API mechanics. The following table summarizes the key official SDKs:
| Language | Package Name | Install Command | Maturity |
|---|---|---|---|
| Node.js | @cpfhub/sdk-nodejs |
npm install @cpfhub/sdk-nodejs |
Stable |
| Python | cpfhub-sdk-python |
pip install cpfhub-sdk-python |
Stable |
| Java | com.cpfhub/sdk-java |
Maven: <dependency><groupId>com.cpfhub</groupId><artifactId>sdk-java</artifactId><version>1.0.0</version></dependency>Gradle: implementation 'com.cpfhub:sdk-java:1.0.0' |
Stable |
Each SDK is designed to align with the idiomatic patterns of its respective language, providing a natural development experience. For detailed usage instructions and API references specific to each SDK, developers should consult the CPFHub API Reference.
Installation
Installing CPFHub SDKs typically involves using the standard package manager for each programming language. The process is designed to be straightforward, allowing developers to quickly set up their development environments.
Node.js
For Node.js projects, the CPFHub SDK can be installed via npm, the default package manager for JavaScript. This command adds the SDK to your project's dependencies:
npm install @cpfhub/sdk-nodejs
After installation, the SDK can be imported into your JavaScript or TypeScript files using require or import statements.
Python
Python developers can install the CPFHub SDK using pip, Python's package installer. This command retrieves the latest version of the SDK from PyPI:
pip install cpfhub-sdk-python
Once installed, the SDK classes and functions can be imported into Python scripts or applications.
Java
For Java projects, the CPFHub SDK is available through popular build tools like Maven and Gradle. Developers need to add the appropriate dependency entry to their project's pom.xml (Maven) or build.gradle (Gradle) file.
Maven Dependency
<dependency>
<groupId>com.cpfhub</groupId>
<artifactId>sdk-java</artifactId>
<version>1.0.0</version>
</dependency>
Gradle Dependency
implementation 'com.cpfhub:sdk-java:1.0.0'
After adding the dependency, the build tool will download the SDK, making its classes available for use in Java code. For specific versioning and additional installation details, refer to the official CPFHub documentation.
Quickstart example
This quickstart example demonstrates how to use the Node.js SDK to retrieve a sample CPF statement using the CPF Statement Retrieval API. Before running this code, ensure you have obtained your API key and secret from your CPFHub developer account and have installed the Node.js SDK.
For security, it is recommended to manage API keys using environment variables or a secure configuration management system rather than hardcoding them directly into source code, as emphasized in general API security practices documented by organizations like Google Developers API Key Best Practices.
const CpfhubClient = require('@cpfhub/sdk-nodejs');
const client = new CpfhubClient({
apiKey: process.env.CPFHUB_API_KEY,
apiSecret: process.env.CPFHUB_API_SECRET,
// For sandbox environment, specify the base URL
baseUrl: 'https://sandbox.cpfhub.sg/api/v1'
});
async function getCpfStatement(nric) {
try {
const statement = await client.statement.retrieve(nric);
console.log('CPF Statement Retrieved:', statement);
return statement;
} catch (error) {
console.error('Error retrieving CPF statement:', error.message);
// Handle specific error types, e.g., not found, unauthorized
if (error.response && error.response.status === 404) {
console.log('Statement for NRIC not found.');
} else {
console.log('An unexpected error occurred.');
}
throw error;
}
}
// Example usage (replace with actual NRIC for testing in sandbox)
const sampleNric = 'S1234567A'; // Use a valid test NRIC from your sandbox setup
getCpfStatement(sampleNric);
This example initializes the CPFHub client with authentication credentials and then calls the statement.retrieve method. The async/await pattern is used for handling asynchronous API calls, and a basic error handling block is included to catch and log potential issues. Further details on specific API endpoints and their parameters can be found in the CPFHub API reference documentation.
Community libraries
While CPFHub focuses on providing and maintaining official SDKs for Node.js, Python, and Java, the open nature of its API allows for the development of community-contributed libraries and wrappers. These community efforts can extend CPFHub's reach to other programming languages or frameworks not officially supported, or provide specialized functionalities that complement the official SDKs.
Community libraries are developed and maintained independently by developers who find value in integrating with CPFHub services. These libraries may vary in terms of their completeness, documentation quality, and maintenance frequency compared to official SDKs. Developers considering the use of community-contributed tools should evaluate their robustness, active development status, and adherence to security best practices. Resources like GitHub often host such projects, and their respective repositories typically provide installation instructions and usage examples. For the most up-to-date and reliable information on integrating with CPFHub, the official CPFHub developer documentation remains the primary resource.