Go SDK
Setup & Initialization
- Install Package:
go get github.com/notificationapi-com/notificationapi-go-server-sdk
- Import:
import (
notificationapi "github.com/notificationapi-com/notificationapi-go-server-sdk"
)
- Initialize:
notificationapi.Init("CLIENT_ID", "CLIENT_SECRET", "base_url")
| Name | Type | Description |
|---|---|---|
CLIENT_ID* | string | Your NotificationAPI account clientId. You can get it from here. |
CLIENT_SECRET* | string | Your NotificationAPI account client secret. You can get it from here. |
base_url* | string | To choose a different region other than default (US). Use https://api.ca.notificationapi.com to access the Canada region, and https://api.eu.notificationapi.com for the EU region. |
* required
Region specific example:
use NotificationAPI\NotificationAPI;
notificationapi = NotificationAPI.new("CLIENT_ID", "CLIENT_SECRET", "https://api.eu.notificationapi.com");
Send
Used to send a notification to the specified user.
Pass content directly without templates
You can ignore the templates and pass the content directly through the API by specifying channel content objects on the request.
parameters := make(map[string]interface{})
notificationapi.Send(
notificationapi.SendRequest{
Type: "order_tracking",
To: notificationapi.User{
Id: "spongebob.squarepants",
Email: "spongebob@squarepants.com",
Number: "+15005550006",
},
Email: map[string]interface{}{
"subject": "Your Krabby Patty Order is Ready!",
"html": "<h1>Order Update</h1><p>Your order is ready for pickup!</p>",
"senderName": "Krusty Krab",
"senderEmail": "orders@krustykrab.com",
},
SMS: map[string]interface{}{
"message": "Your Krabby Patty order is ready for pickup!",
},
InApp: map[string]interface{}{"title": title, "image": image, "url": url},
WebPush: map[string]interface{}{
"title": "Order Update",
"message": "Your Krabby Patty order is ready for pickup!",
"icon": "https://example.com/icon.png", "url": "https://example.com"},
MobilePush: map[string]interface{}{
"title": "Order Update",
"message": "Your Krabby Patty order is ready for pickup!"},
Slack: map[string]interface{}{
"text": "Your Krabby Patty order is ready for pickup!"
},
},
)
With templates and parameters
Use templates you designed in the Dashboard and inject dynamic values with parameters. The server renders your templates per-channel using the values you provide.
This example will send notifications over the channels and templates specified in our dashboard:
To learn more about parameters, see Parameters (Merge Tags), and to learn more about templates, see Templating.
parameters := make(map[string]interface{}) // Change to map[string]interface{}
parameters["item"] = "Krabby Patty Burger"
parameters["address"] = "124 Conch Street"
parameters["orderId"] = "1234567890"
notificationapi.Send(
notificationapi.SendRequest{
Type: "order_tracking",
To: notificationapi.User{
Id: "test_user_id",
Email: "spongebob@squarepants.com",
},
Parameters: parameters,
},
)
Parameters
The send() method accepts an object with the following fields:
| Name | Type | Description |
|---|---|---|
type* | string | The ID of the notification you wish to send. You can find this value from the dashboard. |
to* | object | The user to send the notification to. |
to.id* | string | The ID of the user in your system. Required. This is the only user field required for sending in-app, web push, and mobile push notifications. |
to.email | string | Required for sending email notifications, otherwise optional. |
to.number | string | Required for SMS/CALL notifications, otherwise optional. Valid format: +15005550006. Unformatted US/Canada numbers are also accepted, e.g., (415) 555-1212, 415-555-1212, or 4155551212. |
to.timezone | string | The user’s timezone. Timezone names correspond to the Zone and Link names of the IANA Time Zone Database, such as ‘America/New_York’, ‘America/Denver’, ‘EST’, and ‘UTC’. |
parameters | object | Used to pass in dynamic values into the notification design. Read more: Dynamic Parameters (Merge Tags) |
schedule | string | An ISO 8601 datetime string to schedule the notification for. For example, 2024-02-20T14:38:03.509Z. Read more: Scheduling |
replace | object, string key/value pair | Similar to parameters, but more flexible. Like the programmatic string replace function, this parameter will replace any string in the notification templates with another string. This operation happens on the fly when sending the notification and does not actually modify the templates. This operation is case-sensitive and happens after parameters are injected. |
subNotificationId | string | To break down your notification into multiple categories or groups. Read more: Sub Notifications |
templateId | string | By default, notifications are sent using the default template of each channel. You can permanently change the default template from the dashboard. However, you can also use this parameter to force using a specific template. This is useful in multiple situations: - Design variation: using different wording and design, e.g. “You have new updates” vs. “You don’t have any updates” - White-labeling: using a specific template that you designed for a white-labeled customer - Language: creating and using multiple templates for multiple languages If the provided templateId does not exist for a channel, the default template will be used, and a warning message will be generated. |
forceChannels (deprecated) | string[] | Used to override the channels which are used for the notification. This field only overrides the notification’s channels configuration. It does not override the user preferences. Values available for use are: EMAIL, INAPP_WEB, SMS, CALL, PUSH, WEB_PUSH E.g. forceChannels: ["EMAIL", "SMS"] |
options | object | For configuring additional options |
email | object | Override the email template by providing the complete email content. When provided, this will replace the dashboard template for the email channel. |
email.subject* | string | The email subject line. Required when using email override. |
email.html* | string | The HTML content of the email. Required when using email override. |
email.previewText | string | The email preview text that appears in email clients. |
email.senderName | string | The sender name for the email. |
email.senderEmail | string | The sender email address. |
sms | object | Override the SMS template by providing the complete SMS content. When provided, this will replace the dashboard template for the SMS channel. |
sms.message* | string | The SMS message content. Required when using SMS override. |
sms.autoReply | object | Configure automatic reply to inbound SMS messages. See Inbound Messages for details. |
sms.autoReply.message* | string | The auto-reply message to send when a recipient replies to your SMS. Sent once per recipient phone number per account. Opt-out keywords (STOP, CANCEL, etc.) skip auto-reply. |
inapp | object | Override the in-app notification template by providing the complete in-app content. When provided, this will replace the dashboard template for the in-app channel. |
inapp.title* | string | The in-app notification title. Required when using in-app override. |
inapp.url | string | The URL to navigate to when the in-app notification is clicked. |
inapp.image | string | The image URL for the in-app notification. |
call | object | Override the call notification template by providing the complete call content. When provided, this will replace the dashboard template for the call channel. |
call.message* | string | The call message content. Required when using call override. |
web_push | object | Override the web push notification template by providing the complete web push content. When provided, this will replace the dashboard template for the web push channel. |
web_push.title* | string | The web push notification title. Required when using web push override. |
web_push.message* | string | The web push notification message. Required when using web push override. |
web_push.icon | string | The icon URL for the web push notification. |
web_push.url | string | The URL to navigate to when the web push notification is clicked. |
mobile_push | object | Override the mobile push notification template by providing the complete mobile push content. When provided, this will replace the dashboard template for the mobile push channel. |
mobile_push.title* | string | The mobile push notification title. Required when using mobile push override. |
mobile_push.message* | string | The mobile push notification message. Required when using mobile push override. |
slack | object | Override the Slack notification template by providing the complete Slack content. When provided, this will replace the dashboard template for the Slack channel. |
slack.text* | string | The Slack message text. Required when using Slack override. |
slack.blocks | object[] | Slack Block Kit blocks for rich formatting. Optional when using Slack override. |
Options Object
| Name | Type | Description |
|---|---|---|
email | object | Email options features. |
email.replyToAddresses | string[] | An array of email addresses that will receive responses when recipients reply to the notification email. This enables two-way email communication, allowing recipients to respond directly to notifications. |
email.ccAddresses | string[] | An array of emails to be CC’ed on the email notifications. |
email.bccAddresses | string[] | An array of emails to be BCC’ed on the email notifications. |
email.fromName | string | A string to display as the “From” field in an email. |
email.fromAddress | string | An email address to send the email from. To use a custom
|
email.attachments | | An array of attachments. Each attachment must be either URL-based OR
base64 content-based (mutually exclusive). The API validates this using
a |
apn | object | Additional Apple push notification options. |
apn.expiry | number | The UNIX timestamp representing when the notification should expire. This does not contribute to the 2048 byte payload size limit. An expiry of 0 indicates that the notification expires immediately. |
apn.priority | number | The priority of the notification. If you omit this header, APNs sets the notification priority to 10. Specify 10 to send the notification immediately. Specify 5 to send the notification based on power considerations on the user’s device. Specify 1 to prioritize the device’s power considerations over all other factors for delivery, and prevent awakening the device. |
apn.collapseId | string | An identifier you use to merge multiple notifications into a single notification for the user. Typically, each notification request displays a new notification on the user’s device. When sending the same notification more than once, use the same value in this header to merge the requests. The value of this key must not exceed 64 bytes. |
apn.threadId | string | Provide this key with a string value that represents the app-specific identifier for grouping notifications. If you provide a Notification Content app extension, you can use this value to group your notifications together. For local notifications, this key corresponds to the threadIdentifier property of the UNNotificationContent object. |
apn.badge | number | Include this key when you want the system to modify the badge of your app icon. If this key is not included in the dictionary, the badge is not changed. To remove the badge, set the value of this key to 0. |
apn.sound | string | Include this key when you want the system to play a sound. The value of
this key is the name of a sound file in your app’s main bundle or in the
Library/Sounds folder of your app’s data container. If the sound file
cannot be found, or if you specify here . |
apn.contentAvailable | boolean | Include this key with a value of 1 to configure a background update notification. When this key is present, the system wakes up your app in the background and delivers the notification to its app delegate. For information about configuring and handling background update notifications, see here . |
fcm | object | Additional Firebase Cloud Messaging push notification options. |
fcm.android | object | Additional Android push notification options. |
fcm.android.collapseKey | string | This parameter identifies a group of messages (e.g., with
|
fcm.android.priority | string | Sets the priority of the message. Valid values are |
fcm.android.ttl | string | This parameter specifies how long (in seconds) the message should be kept in FCM storage if the device is offline. The maximum time to live supported is 4 weeks, and the default value is 4 weeks. For more information, see Setting the lifespan of a message . |
To use a custom fromAddress from your domain, you’ll need to verify your domain first. See your NotificationAPI dashboard for Domain Verification setup instructions. We strongly recommend you to verify your domain before sending emails from production.
Size Limitation
When using parameters (merge tags) in the body of your notifications, it is recommended that the size of the combined parameters not exceed 150 KB. Exceeding this limit may result in delivery failure, or an API error.
identifyUser
Stores the end-user information for a given user.
By using this function, you can omit the contact information when using send(). NotificationAPI will automatically lookup the user’s stored email or phone number and use it for sending the notification.
number := "+15005550006"
user := NotificationAPI.User{
Id: "spongebob.squarepants",
Email: "spongebob@squarepants.com",
Number: &number,
WebPushTokens: &[]NotificationAPI.UserWebPushToken{
{
Sub: struct {
Endpoint string `json:"endpoint,omitempty"`
Keys struct {
P256DH string `json:"p256dh,omitempty"`
Auth string `json:"auth,omitempty"`
} `json:"keys,omitempty"`
}{
Endpoint: "https://fcm.googleapis.com/fcm/send/fCs_4iba0Ao:APA91bGFdaU7I3****JMH_KeZwk1Xi",
Keys: struct {
P256DH string `json:"p256dh,omitempty"`
Auth string `json:"auth,omitempty"`
}{
P256DH: "zP2xFu3hMc2vNH5E2nuKkyjpZydvCk9llRUY2kP4****9aSlKcoadSV2UbvMRQ",
Auth: "CXEFun************tYe8g",
},
},
},
},
}
var params notificationapi.SetUserPreferencesRequest
NotificationAPI.IdentifyUser(user)
Parameters
| Name | Type | Description |
|---|---|---|
id* | string | The ID of the user in your system. Required. |
email | string | Required for sending email notifications, otherwise optional. |
number | string | Required for SMS/CALL notifications, otherwise optional. Valid format: +15005550006. Unformatted US/Canada numbers are also accepted, e.g., (415) 555-1212, 415-555-1212, or 4155551212. |
timezone | string | The user’s timezone. Timezone names correspond to the Zone and Link names of the IANA Time Zone Database, such as ‘America/New_York’, ‘America/Denver’, ‘EST’, and ‘UTC’. |
pushTokens | object[] | Tokens which are required to send mobile push notifications, a user can have multiple devices and a push token is required for each device. |
webPushTokens | object[] | Tokens which are required to send web push notification, a user can have multiple devices and a web push token is required for each device. |
pushTokens object
| Name | Type | Description |
|---|---|---|
type* | string | [apn,fcm] The provider token is to be associated with. Required. |
token* | string | The full token string. Required. |
device* | object | Information about the device the token is associated with. Required. |
device object
| Name | Type | Description |
|---|---|---|
app_id | string | Id of the application the token is used for. |
ad_id | string | Id of the advertising identifier. |
device* | string | Id of the device the token is associated with. Required. |
platform | string | The device platform i.e. android, ios. |
manufacturer | string | The device manufacturer. |
model | string | The device model . |
webPushTokens object
| Name | Type | Description |
|---|---|---|
sub* | object | Web push subscription. Required. |
Web push subscription (sub) object
| Name | Type | Description |
|---|---|---|
endpoint* | string | The string value containing the endpoint associated with the push subscription. Required. |
keys* | object | The keys associated with the push subscription. Required. |
Web push subscription keys object
| Name | Type | Description |
|---|---|---|
p256dh* | string | An Elliptic curve Diffie–Hellman public key on the P-256 curve (that is, the NIST secp256r1 elliptic curve). The resulting key is an uncompressed point in ANSI X9.62 format. Required. |
auth* | string | An authentication secret, as described in Message Encryption for Web Push. Required. |
setUserPreferences
Used to set a user’s notification preferences. This function will override the user’s existing preferences.
jsonParams := `[
{
type: "new_order",
channel: "INAPP_WEB",
delivery: "weekly"
},
{
type: "order_tracking",
channel: "SMS",
delivery: "weekly"
}
]`
var params notificationapi.SetUserPreferencesRequest
json.Unmarshal([]byte(jsonParams), ¶ms)
notificationapi.SetUserPreferences("userId", params)
Parameters
| Name | Type | Description |
|---|---|---|
userId* | string | The ID of the user in your system. Required. |
preferences* | object[] | An array of preference objects. |
Preference Object
| Name | Type | Description |
|---|---|---|
type* | string | The ID of the notification you wish to send. You can find this value from the dashboard. |
channel | string | The channel you wish to set the preference for. Accepted values: EMAIL, INAPP_WEB, SMS, CALL, PUSH, WEB_PUSH |
delivery | string | The method for delivering the notification. Accepted values: off, instant, hourly, daily, weekly, monthly |
deleteUserPreferences
Used to delete any stored preferences for a user and type or subNotificationId.
jsonParams := `{
Type: "order_tracking",
UserId: "spongebob.squarepants"
}`
var params notificationapi.DeleteUserPreferencesRequest
json.Unmarshal([]byte(jsonParams), ¶ms)
notificationapi.DeleteUserPreferences(params);
Parameters
| Name | Type | Description |
|---|---|---|
userId* | string | The ID of the user in your system. Required. |
type* | string | The ID of the notification in NotificationAPI. Required. |
subNotificationId | string | The subNotificationId is used to specify further subcategories within a notification. |
updateInAppNotification
Used to update the opened, archived, … of an inapp notification.
notificationapi.UpdateInAppNotification("spongebob.squarepants", map[string]interface{}{
"trackingIds": []string{"sampleTrackingId"},
"opened": "1970-01-01T00:00:00.000Z",
"clicked": "1970-01-01T00:00:00.000Z",
"archived": "1970-01-01T00:00:00.000Z",
"actioned1": "1970-01-01T00:00:00.000Z",
"actioned2": "1970-01-01T00:00:00.000Z",
"reply": map[string]interface{}{
"date": "1970-01-01T00:00:00.000Z",
"message": "nice!",
},
})
Parameters
| Name | Type | Description |
|---|---|---|
userId* | string | The ID of the user in your system. Required. |
params* | object | The inapp notification data you want to update. Required. |
params
| Name | Type | Description |
|---|---|---|
trackingIds* | string[] | The Tracking IDs of the notifications you want to update. Required. |
opened | string | A string representing the time when the notification was opened. Optional. |
clicked | string | A string representing the time when the notification was clicked. Optional. |
archived | string | A string representing the time when the notification was archived. Optional. |
actioned1 | string | A string representing the time when the first action associated with the notification was taken. Optional. |
actioned2 | string | A string representing the time when the second action associated with the notification was taken. Optional. |
reply.date | string | A string representing the time of the reply to the notification. Optional. |
reply.message | string | A string representing the content of the reply message. Optional. |
retract
Only applicable to in-app notifications. This function helps you delete all notifications of a certain type for a user.
jsonParams := `{
Type: "order_tracking",
UserId: "spongebob.squarepants"
}`
var params notificationapi.RetractRequest
json.Unmarshal([]byte(jsonParams), ¶ms)
notificationapi.Retract(params);
| Name | Type | Description |
|---|---|---|
type* | string | The ID of the notification you wish to retract. You can find this value from the dashboard. |
userId* | string | The ID of the user in your system. Required. |
secondaryId | string | For when you want to delete a specific subNotificationId. |
updateSchedule
This function enables you to update a scheduled notification by providing a trackingId.
updateReq := NotificationAPI.UpdateScheduleRequest{
Schedule: "2024-02-20T14:38:03.509Z",
}
trackingId := "172cf2f4-18cd-4f1f-b2ac-e50c7d71891c"
notificationapi.UpdateSchedule(trackingId, updateReq)
| Name | Type | Description |
|---|---|---|
trackingId* | string | The tracking ID of the scheduled notification you wish to update. You can find this value from the dashboard. |
deleteSchedule
This function allows you to delete a scheduled notification by providing a trackingId.
trackingId := "172cf2f4-18cd-4f1f-b2ac-e50c7d71891c"
notificationapi.DeleteSchedule(trackingId)
| Name | Type | Description |
|---|---|---|
trackingId* | string | The tracking ID of the scheduled notification you wish to delete. You can find this value from the dashboard. |
queryLogs
This function allows you to run queries on the logs of your send requests.
Query results are typically available in real-time (under 60 seconds), but in some cases may take up to 15 minutes.
params := map[string]interface{}{
// [startTime, endTime] unix timestamps (ms)
"dateRangeFilter": []int64{1719600830559, 1719600840559},
"notificationFilter": []string{"order_tracking"},
"channelFilter": []string{"EMAIL"},
"userFilter": []string{"abcd-1234"},
"statusFilter": []string{"SUCCESS"},
"trackingIds": []string{"172cf2f4-18cd-4f1f-b2ac-e50c7d71891c"},
"requestFilter": []string{`request.parameters.item="Krabby Patty Burger"`},
"envIdFilter": []string{"6ok6imq9unr2budgiebjdaa6oi"},
}
notificationAPI.QueryLogs(params)
| Name | Type | Description |
|---|---|---|
dateRangeFilter* | number[] | Required. A tuple of [startTime, endTime] unix timestamps (ms) representing the query time range. Must contain exactly 2 items. |
notificationFilter | string[] | Filters logs by specific notification IDs. Optional. This allows you to retrieve logs for particular notifications. |
channelFilter | Channels[] | Filters logs by specific channels. Optional. This allows you to retrieve logs for certain channels like EMAIL, SMS, etc. |
userFilter | string[] | Filters logs by specific user IDs. Optional. This allows you to retrieve logs for particular users. |
statusFilter | string[] | Filters logs by specific statuses. Optional. This allows you to retrieve logs with particular statuses like sent, failed, etc. |
trackingIds | string[] | Filters logs by specific tracking IDs. Optional. This allows you to retrieve logs for particular tracking events. |
requestFilter | string[] | Filters logs by specific body request of your send request. Optional. This allows you to retrieve logs for particular requests. |
envIdFilter | string[] | Filters logs by specific environment IDs. Optional. This allows you to retrieve logs for particular environments. |
customFilter | string | A custom filter for querying logs. Optional. This allows for more advanced and flexible querying of logs. Note: custom filter overwrites all the filters. |