SDKs overview
Open Government, Singapore provides a suite of APIs designed to facilitate the development of digital services and applications within the public sector. To streamline the integration process, both official and community-contributed Software Development Kits (SDKs) and libraries are available. These SDKs abstract the underlying HTTP requests and authentication mechanisms, allowing developers to interact with services like Singpass and Myinfo using familiar programming language constructs, rather than directly managing API endpoints and request formats.
The official SDKs are primarily maintained by the Government Technology Agency (GovTech) and are documented on the official developer.gov.sg portal. These resources aim to reduce the boilerplate code required for common tasks, such as securely authenticating users via Singpass or accessing verified personal data through Myinfo. The focus is on enabling efficient and compliant development of applications that leverage the Singapore government's digital infrastructure.
Official SDKs by language
Open Government, Singapore offers official libraries and SDKs primarily for Python and Node.js, reflecting common choices in web and backend development. These SDKs are designed to provide direct access to key government services, including authentication and data exchange. While dedicated SDKs for all APIs may not be present, the developer portal provides comprehensive documentation, code examples, and Postman Collections, which can be adapted into libraries for other languages.
The following table outlines the officially supported SDKs and their typical usage:
| Language | Package/Library | Description | Maturity |
|---|---|---|---|
| Python | govtech-myinfo-sdk-python |
Facilitates integration with the Myinfo API for retrieving verified personal data. | Official, Stable |
| Node.js | govtech-myinfo-sdk-nodejs |
Provides Node.js bindings for the Myinfo API, supporting data retrieval and handling. | Official, Stable |
| Python | govtech-singpass-sdk-python |
Enables integration with Singpass for secure user authentication. | Official, Stable |
| Node.js | govtech-singpass-sdk-nodejs |
Node.js library for integrating Singpass authentication into applications. | Official, Stable |
Installation
Installation of Open Government, Singapore's official SDKs follows standard package manager procedures for Python and Node.js. Developers are encouraged to use virtual environments for Python projects and local node_modules for Node.js projects to manage dependencies effectively.
Python
To install a Python SDK, such as the Myinfo SDK, use pip:
pip install govtech-myinfo-sdk-python
For the Singpass SDK:
pip install govtech-singpass-sdk-python
Ensure you have Python 3.6 or newer installed. It's recommended to create a virtual environment first:
python -m venv myenv
source myenv/bin/activate # On Windows, use `myenv\Scripts\activate`
pip install govtech-myinfo-sdk-python
Node.js
To install a Node.js SDK, such as the Myinfo SDK, use npm:
npm install govtech-myinfo-sdk-nodejs
For the Singpass SDK:
npm install govtech-singpass-sdk-nodejs
Ensure you have Node.js and npm installed. Navigate to your project directory and run the installation command.
Configuration
After installation, SDKs typically require configuration with API credentials (e.g., client ID, client secret, private keys) obtained from the developer portal's API key management section. These credentials are used to authenticate your application with the respective government APIs. It is critical to manage these credentials securely, often using environment variables or a secure configuration service, rather than hardcoding them directly into the application's source code.
Quickstart example
This example demonstrates a basic interaction with the Myinfo API using the Python SDK to fetch a user's profile data after successful Singpass authentication. This assumes that the user has already authenticated via Singpass and an authorization code has been obtained.
from govtech_myinfo_sdk_python import MyInfoClient
import os
# Configuration (replace with your actual credentials and endpoint)
CLIENT_ID = os.getenv('MYINFO_CLIENT_ID')
CLIENT_SECRET = os.getenv('MYINFO_CLIENT_SECRET')
REDIRECT_URI = 'https://your-app.com/callback'
AUTH_CODE = 'YOUR_SINGPASS_AUTH_CODE' # This comes from Singpass callback
PRIVATE_KEY_PATH = 'path/to/your/private_key.pem'
MYINFO_PUBLIC_CERT_PATH = 'path/to/myinfo/public_cert.pem'
# Initialize MyInfo client
try:
with open(PRIVATE_KEY_PATH, 'rb') as f_priv,
open(MYINFO_PUBLIC_CERT_PATH, 'rb') as f_pub:
private_key = f_priv.read()
myinfo_public_cert = f_pub.read()
client = MyInfoClient(
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
redirect_uri=REDIRECT_URI,
private_key=private_key,
myinfo_public_cert=myinfo_public_cert,
# Set to True for sandbox environment, False for production
is_production=False
)
# Get access token using the authorization code
access_token_response = client.get_access_token(AUTH_CODE)
access_token = access_token_response.get('access_token')
if access_token:
print(f"Access Token: {access_token}")
# Get user profile data using the access token
# The 'attributes' parameter specifies which data fields to retrieve
profile_data = client.get_person_data(access_token, attributes=['name', 'email', 'mobileno'])
print("User Profile Data:")
print(profile_data)
else:
print("Failed to retrieve access token.")
except Exception as e:
print(f"An error occurred: {e}")
This example illustrates how to exchange an authorization code for an access token and then use that token to retrieve specific user attributes. The is_production flag should be managed carefully, ensuring it is set correctly for deployment environments. The actual values for CLIENT_ID, CLIENT_SECRET, REDIRECT_URI, PRIVATE_KEY_PATH, and MYINFO_PUBLIC_CERT_PATH must be replaced with your application's specific details and securely loaded, typically from environment variables or a secrets management service.
For detailed API specifications and available attributes, refer to the official Myinfo API specification.
Community libraries
While Open Government, Singapore provides official SDKs for core services, the broader developer community may contribute libraries or wrappers for additional languages or specific use cases. These community-driven projects can offer alternative implementations or extend functionality beyond the official offerings. However, community libraries typically do not carry the same level of official support or guarantees regarding security and maintenance as the SDKs directly provided by GovTech.
Developers considering community libraries should review their source code, check for active maintenance, and understand their licensing terms. Forums and developer communities, such as those found on GitHub or local developer groups, are good places to discover and evaluate such contributions. The official Open Government, Singapore developer community resources may also highlight notable community projects or provide platforms for collaboration.
When integrating any third-party library, it's crucial to perform due diligence, especially when handling sensitive government data or implementing authentication flows. This includes verifying the library's adherence to security best practices and compliance with relevant regulations like the Personal Data Protection Act (PDPA) in Singapore. For critical applications, official SDKs or direct API integration using well-vetted HTTP clients (e.g., Python's requests library or Node.js's axios) might be preferred to maintain full control over security and compliance aspects, as outlined in general API security guidelines such as those provided by OAuth 2.0 specifications.