This page is meant for developers, vendors, and IT administrators to understand how to generate the bearer token to access our API to create/send and fetch status of letters.
Bearer authentication (also called token authentication) is an HTTP authentication scheme that involves security tokens called bearer tokens. LetterSG uses bearer authentication.
To generate the token, click on "API Integration" in the navigation bar. From there, click on Generate API key and copy the token. Use this key to start using LetterSG's API.
Authentication
LetterSG's API uses APIKey for authentication. User can view and manage API Keys in LetterSG API Dashboard.
Staging secret keys will have test_v1_version prefix.
Production secret keys will have live_v1_version prefix.
Authentication to the API is performed via bearer auth.
All API requests must be made over HTTPS. Calls made over plain HTTP will fail and requests without authentication will also fail.
To verify that your API key is working, send a curl request:
LetterSG API uses conventional API Error to indicate the success or failure of an API request.
Status Codes
Description
200 - OK
Everything worked as expected.
302 - Redirect
(PDF endpoint) Redirects to the presigned URL for PDF.
400 - Bad Request
The request was unacceptable, often due to missing a required parameter.
401 - Unauthorized
No valid API key provided.
402 - Request Failed
The parameters were valid but the request failed.
404 - Not Found
The requested resource doesn't exist.
429 - Too Many Requests
Too many requests hit the API too quickly. We recommend an exponential backoff of your requests.
500, 502, 503, 504 - Server Errors
Something went wrong on LetterSG's API end.
Rate Limits
Unless otherwise stated, LetterSG allows up to 20 requests per second per user (subject to change).
Endpoints
Environment
Endpoint
Production
https://letters.gov.sg/api/
Staging
https://staging.letters.gov.sg/api/
Get All Templates
To see all templates that you have access to, send a GET request to list all templates. This endpoint also returns template fields that you will need to provide when generating the letters for each template.
GET /v1/templates
Request body:
Parameter
Type
Default value
Required?
Description
limit
number
100
No, optional
Maximum number of templates to be returned at once
offset
number
0
No, optional
Offset of the first template returned in the collection.
Returns:
// list of all templates that the user has access to
[
templates: {
templateId: number
fields: string[]
name: string
createdAt: string
updatedAt: string
}[],
count: number
]
Sample request:
# replace live_v1_YOUR_API_KEY with your actual API key
curl --location \
--request GET 'https://letters.gov.sg/api/v1/templates?limit=2&offset=5' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY'
To see the details of a template, send a GET request with the template ID of the template. This endpoint returns the template fields you need to provide when generating a letter.
GET /v1/templates/:id
Request body
Parameter
Type
Required?
id
number
Yes, required
ID of the template
Returns:
// details of template
{
templateId: number
fields: string[]
name: string
createdAt: string
updatedAt: string
}
Sample request:
# replace live_v1_YOUR_API_KEY with your actual API key
curl --location \
--request GET 'https://letters.gov.sg/api/v1/templates/155' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY'
To create a new letter, send a POST request specifying the template you would like to use, and the provide the relevant template fields for the letter. If you are unsure which template fields are required for the template, see Get Template Details By Id.
The endpoint returns the link to the created letter, the public id of the letter, and the html of the created letter. If you need to query the status of the letter or retrieve information about the letter, please store the public id of the letter as this is the unique identifier for this letter.
POST /v1/letters
Request body:
Property
Type
Required?
Description
templateId
number
Yes, required
ID of the template
letterParams
JSON
Yes, required
Key-value pairs of the letter, based on the params contained in the template.
If params values are not available, param must still be provided but you may use an empty space to indicate that the param is not available e.g."name": " "
notificationParams
JSON
No, optional
See below for notificationParams object format
Notification params, if provided, should look like so:
Property
Type
Required
Description
recipient
string
Yes, required
Contact of recipient, can be either phone number or email, provided as string.
Phone numbers should start with either 8 or 9. Only local phone numbers supported for now.
notificationMethod
ENUM ("SMS" or "EMAIL")
Yes, required
Case sensitive
Returns:
// created letter
{
publicId: string
issuedLetter: string
letterLink: string
createdAt: string
firstReadAt: string
notificationStatus?: NotificationStatus
recipient?: string
}
Sample request:
# replace live_v1_YOUR_API_KEY with your actual API key
# sample request to create a letter only, without SMS or EMAIL notification
curl --location 'https://letters.gov.sg/api/v1/letters' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY' \
--data '{
"templateId": 155,
"letterParams": {
"recipient_name": "Maxine Marcella"
}
}'
# sample request with SMS notification
# replace YOUR_PHONE_NUMBER_STARTING_WITH_8_OR_9 with your phone number
# this sends a real SMS out, please test with your own phone number
curl --location 'https://letters.gov.sg/api/v1/letters' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY' \
--data '{
"templateId": 155,
"letterParams": {
"recipient_name": "Maxine Marcella"
},
"notificationParams": {
"recipient": "YOUR_PHONE_NUMBER_STARTING_WITH_8_OR_9",
"notificationMethod": "SMS"
}
}'
# sample request with EMAIL notification
# replace YOUR_EMAIL with your email address
# this sends a real email out, please test with your own email address
curl --location 'https://letters.gov.sg/api/v1/letters' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY' \
--data '{
"templateId": 155,
"letterParams": {
"recipient_name": "Maxine Marcella"
},
"notificationParams": {
"recipient": "YOUR_EMAIL",
"notificationMethod": "EMAIL"
}
}'
// extra letter param in request, serial_number
{
"message": "Invalid letter params.",
"error": [
{
"id": 0,
"param": "serial_number",
"message": "Invalid attribute in param",
"displayedErrorMessage": " is an extra field"
}
]
}
// missing letter param in request, recipient_name
{
"message": "Invalid letter params.",
"error": [
{
"id": 0,
"param": "recipient_name",
"message": "Missing param",
"displayedErrorMessage": " field is missing"
}
]
}
(Bulk) Create letters
To create a batch of letters, send a POST request specifying the template you would like to use, and the provide a list of template fields, one set of template fields for each letter to be generated. If you are unsure which template fields are required for the template, see Get Template Details By Id.
The endpoint returns the id of the created batch of letters. If you need to query the status of the batch, please store the id of the batch as this is the identifier for this batch.
This endpoint is generates a max of 500 letters at one time. Supported TPS: 1 transaction per second per user.
Please contact us if you need us to increase the rate limits.
POST /v1/letters/bulks
Request body:
Property
Type
Required?
templateId
number
Yes, required
ID of the template
lettersParams
JSON
Yes, required
An array containing key-value pairs for each letter, based on the params contained in the template.
If param values are not available, param must still be provided but you may use an empty space to indicate that the param is not available e.g."name": " "
notificationMethod
string
No, optional
If provided, it specifies the notification channel for all recipients in the batch. Must be one of the following: "EMAIL", "SMS"
recipients
JSON
No, optional
An array of recipients that correspond to the letters. This list is required if a notification is to be sent. Each recipient will be notified via the method specified at the batch level.
If notificationMethod is "EMAIL" or "SMS", recipients must be provided.
Contact of recipient can be either phone number or email, provided as strings.
Phone numbers should start with either 8 or 9. Only local phone numbers supported for now.
Returns:
// created letter
{ batchId: number }
Sample request:
# sample request to create letters only, without SMS or EMAIL notification
curl --location 'https://letters.gov.sg/api/v1/letters' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY' \
--data '{
"templateId": 155,
"lettersParams": [
{
"recipient_name": "Test1"
},
{
"recipient_name": "Test2"
}
]
}'
# sample request with SMS notifications
# replace YOUR_PHONE_NUMBER with your phone numbers
# this sends a real sms out, please test with your own phone numbers
curl --location 'https://letters.gov.sg/api/v1/letters' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY' \
--data '{
"templateId": 155,
"lettersParams": [
{
"recipient_name": "Test1"
},
{
"recipient_name": "Test2"
}
],
"notificationMethod": "SMS",
"recipients": ["YOUR_PHONE_NUMBER", "YOUR_PHONE_NUMBER"]
}'
# sample request with EMAIL notification
# replace YOUR_EMAIL with your email address
# this sends a real email out, please test with your own email address
curl --location 'https://letters.gov.sg/api/v1/letters' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY' \
--data '{
"templateId": 155,
"lettersParams": [
{
"recipient_name": "Test1"
},
{
"recipient_name": "Test2"
}
],
"notificationMethod": "EMAIL",
"recipients": ["YOUR_EMAIL", "YOUR_EMAIL"]
}'
Response:
{
batchId: 3
}
Potential error responses (non-exhaustive):
// 404 Not Found errors
// template does not exist
{
"message": "Template not found",
}
// 400 Bad Request errors
// invalid, missing or extra params
{
"message": "Invalid letter params/ recipients",
"errors": [
{
"id": 0,
"errorType": "Invalid attribute in param",
"message": "hello is an extra field"
},
{
"id": 0,
"errorType": "Missing param",
"message": "recipient_name field is missing"
},
{
"id": 1,
"errorType": "Missing param",
"message": "serial_number field is missing"
}
]
}
{
"message": "Notification method must be one of 'EMAIL', 'SMS'",
}
// invalid recipients or missing recipients
{
"message": "Invalid letter params/ recipients",
"errors": [
{
"id": 1,
"errorType": "Invalid phone number",
"message": "Phone Number should be local SG handphone number"
},
{
"id": 2,
"errorType": "Missing param",
"message": "Phone Number field is missing"
},
}
Get Letter Metadata
To retrieve the status of the letter, send a GET request with the public id of the letter that you would like to retrieve metadata for. You are only able to retrieve the metadata of letters that you have access to.
GET /v1/letters/:publicId
Request Query Parameters:
Parameter
Type
Required?
Description
publicId
string
Yes, required
Letter ID for fetching the status e.g. xxyyy-12345-ddsss-d3454
(Optional) Timestamp of first read of letter, if letter is read
2022-09-19T03:31:00.131Z
notificationStatus
(Optional) Status of the notification that was sent, this could be "SENT", "FAILED", "INVALID_RECIPIENT", "PENDING"
"SENT"
recipient
(Optional) String of the recipient's info
abcss@example.com
Sample Request:
# replace live_v1_YOUR_API_KEY with your actual API key
curl --location \
--request GET 'https://letters.gov.sg/api/v1/letters/2here-8o5td-9g4b6-xfd86' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY'
To retrieve the status of the letter batch, send a GET request with the batch id that you would like to retrieve metadata for. You are only able to retrieve the metadata of batches that you have access to.
To download the issued letter as a PDF for archiving, send a GET request with the publicId of the letter.
Note: We may choose to further rate limit this endpoint in the future. This endpoint currently is offered in beta, and may not be always offered as a synchronous response.
GET /v1/letters/:publicId/pdfs
Request Query Parameters:
Parameter
Type
Default value
Required?
publicId
string
nil
Yes, required
Returns:
Redirects to presigned URL link with a 1-hour expiry (returning a 302 status).
// url to download link
{
presignedUrl: string
}
Sample request:
# downloads PDF as 2here-8o5td-9g4b6-xfd86.pdf on your device
# replace live_v1_YOUR_API_KEY with your actual API key
curl --location \
--request GET 'https://letters.gov.sg/api/v1/letters/2here-8o5td-9g4b6-xfd86/pdfs' \
--header 'Authorization: Bearer live_v1_YOUR_API_KEY' --output 2here-8o5td-9g4b6-xfd86.pdf
Keen on APIs?
For more information on integrating your systems with LetterSG, reach out to us at support@letters.gov.sg