Lists

Methods to create and manage lists using go SDK

List methods lets you manage a list and subscribers in that list. You can then send bulk messages to all the subscribers in the list with a single API call.


Create list

package main

import (
  "context"
  "log"

  suprsend "github.com/suprsend/suprsend-go"
)

func main() {
  // Initialize SDK
	opts := []suprsend.ClientOption{
    // suprsend.WithDebug(true),
  }
  suprClient, err := suprsend.NewClient("__api_key__", "__api_secret__", opts...)
  if err != nil {
    log.Println(err)
  }
	ctx := context.Background()
  // ================= Create subscriber list
	subscriberListCreated, err := suprClient.SubscriberLists.Create(ctx, &suprsend.SubscriberListCreateInput{
		ListId:          "users-with-prepaid-vouchers-1", // max length 64 characters
		ListName:        "Users With Prepaid Vouchers above $250",
		ListDescription: "Users With Prepaid Vouchers above $250",
	})
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(subscriberListCreated)
}
PropertyDescription
list_idmax 64 characters and should contains alphanumeric characters(a-z, 0-9), hyphen (-) and underscode(_).
list_name (Optional)Name of the list. This is for your reference and can be referred to understand what this list is for.
list_description (Optional)Description of the list to identify what type of users belong to this list.

Get list details

subscriberListData, err := suprClient.SubscriberLists.Get(ctx, "_list_id_")
{
	"list_id": "list-id",
	"list_name": "List Name",
  "updated_at": "2022-12-18T10:40:27.268417+00:00",
	"list_description": "List description"
}

Get list of lists

allSubscriberList, err := suprClient.SubscriberLists.GetAll(ctx, &suprsend.SubscriberListAllOptions{)) // default minit 20
allSubscriberList, err := suprClient.SubscriberLists.GetAll(ctx, &suprsend.SubscriberListAllOptions{Limit: 10, Offset: 0}) // limit 10
 //  max limit 1000

Add subscribers to list

addDistinctIds := []string{"distinct_id_1", "distinct_id_2"}
addResponse, err := suprClient.SubscriberLists.Add(ctx, "users-with-prepaid-vouchers-1", addDistinctIds)
if err != nil {
  log.Fatalln(err)
}
log.Println(addResponse)

Remove subscribers from list

removeDistinctIds := []string{"distinct_id_1", "distinct_id_2"}
removeResponse, err := suprClient.SubscriberLists.Remove(ctx, "users-with-prepaid-vouchers-1", removeDistinctIds)
if err != nil {
  log.Fatalln(err)
}
log.Println(removeResponse)

Delete list

deleteListResp, err := suprClient.SubscriberLists.Delete(ctx, "users-with-prepaid-vouchers-1")
if err != nil {
  log.Fatalln(err)
}
log.Println("delete list resp: ", deleteListResp)

Replace users from the list

In case you want to refresh list with a new set of users completely, you can replace users by creating a draft version of the list and updating users in it.

1. Start Sync to create draft version of the list

This method will create a draft version of the list where you can add the new set of users to replace users.

// start sync
newVersion, err := suprClient.SubscriberLists.StartSync(ctx, "users-with-prepaid-vouchers-1")
if err != nil {
  log.Fatalln(err)
}
log.Println("start sync resp: ", newVersion)

versionId := newVersion.VersionId

2. Add Subscribers to draft list

You can use this method to add subscribers to List draft version created in Step 1. You'll get version_id in start sync response.

// ================= Add users to a draft list (with versionId)
addDistinctIds := []string{"id-399999", "id-399998"}
addResponse, err := suprClient.SubscriberLists.AddToVersion(ctx, "users-with-prepaid-vouchers-1", versionId, addDistinctIds)
if err != nil {
  log.Fatalln(err)
}
log.Println("add to version resp:", addResponse)

3. Remove Subscribers from draft list

You can use this method to remove subscribers from List draft version created in Step 1. You'll get version_id in start sync response.

// ================= remove users from a list
removeDistinctIds := []string{"distinct_id_1", "distinct_id_2"}
removeResponse, err := suprClient.SubscriberLists.RemoveFromVersion(ctx, "users-with-prepaid-vouchers-1", versionId, removeDistinctIds)
if err != nil {
  log.Fatalln(err)
}
log.Println("remove from version resp: ", removeResponse)

4. Finish Sync to make the draft version live

Once your subscribers are updated in the list, use this method to finish sync and make the draft version updated in above steps live.

// finish sync
finishSyncResp, err := suprClient.SubscriberLists.FinishSync(ctx, "users-with-prepaid-vouchers-1", versionId)
if err != nil {
  log.Fatalln(err)
}
log.Println("finish sync resp: ", finishSyncResp)

Delete Draft list

You can also delete draft list if it's created by mistake.

deleteVersionResp, err := suprClient.SubscriberLists.DeleteVersion(ctx, "users-with-prepaid-vouchers-1", tempListVersion.VersionId)
if err != nil {
  log.Fatalln(err)
}
log.Println("delete version resp: ", deleteVersionResp)