API Documentation

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 Token & API Key Generation

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:

This request gets the template details for template 155, our sample template OGP - Certificate of Curiosity.

You should see a response containing the details for template 155:

Errors

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.

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:

Sample request:

Response:

Get Template Details by ID

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.

Request body

Parameter
Type
Required?

id

number

Yes, required

ID of the template

Returns:

Sample request:

Response:

(Single) Create Letter

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.

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:

Sample request:

Response:

Potential error responses (non-exhaustive):

(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.

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:

Sample request:

Response:

Potential error responses (non-exhaustive):

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.

Request Query Parameters:

Parameter
Type
Required?
Description

publicId

string

Yes, required

Letter ID for fetching the status e.g. xxyyy-12345-ddsss-d3454

Returned object:

Explanation of fields returned:

Field Returned
What it means
Example

publicId

Letter ID for fetching the read status

xxyyy-12345-ddsss-d3454

letterLink

Original link of the letter

https://letters.gov.sg/letters/public-letter-id

createdAt

Timestamp of letter creation

firstReadAt

(Optional) Timestamp of first read of letter, if letter is read

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

Sample Request:

Returns:

Get Batch Metadata

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.

Request Query Parameters:

Parameter
Type
Required?
Description

id

number

Yes, required

ID of batch

Returned object:

Explanation of fields returned:

Field Returned
What it means
Example

batchId

ID of batch issued

40091

templateId

ID of template used to issue batch

155

totalCount

Total number of letters in batch

readCount

Number of letters that have been read in batch

notificationMethod

(Optional) Method that letters sent via, could be "SMS", "EMAIL"

"SMS"

letterPublicIds

Array of public id of the letters in the batch.

You may use letter publicId to retrieve metadata about the letter

Sample request:

Response:

Get Downloadable Letter PDF (Beta)

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.

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).

Sample request:

Others

How can I whitelist LetterSG / What is LetterSG's IP address? LetterSG endpoints reside in the internet zone, so you can make the request with your API key. As we are being served by Cloudflare DNS, you may whitelist this range of Cloudflare's IP addresses (see here).

Keen on APIs?

For more information on integrating your systems with LetterSG, reach out to us at [email protected]

Last updated

Was this helpful?