Skip to content

About

Go SDK for MailerLite

Topics

Resources

Stars

6 stars

Watchers

3 watching

Forks

Latest commit

 

History

147 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MailerLite Golang SDK

MIT licensed Go Reference

Table of Contents

Installation

We recommend using this package with golang modules

$ go get github.com/mailerlite/mailerlite-go

Usage

Subscribers

Get a list of subscribers

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListSubscriberOptions{
		Limit:  200,
		Page:   0, 
		Filters: &[]mailerlite.Filter{{
			Name:  "status", 
			Value: "active",
		}},
	}

	subscribers, _, err := client.Subscriber.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(subscribers.Meta.Total)
}

Get a single subscriber

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	getOptions := &mailerlite.GetSubscriberOptions{
		SubscriberID: "subscriber-id",
		//Email: "client@example.com"
	}

	subscriber, _, err := client.Subscriber.Get(ctx, getOptions)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(subscriber.Data.Email)
}

Count all subscribers

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	count, _, err := client.Subscriber.Count(ctx)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(count.Total)
}

Create/Upsert a subscriber

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	subscriber := &mailerlite.UpsertSubscriber{
		Email: "example@example.com",
		Fields: map[string]interface{}{
			"city": "Vilnius",
		},
	}

	newSubscriber, _, err := client.Subscriber.Upsert(ctx, subscriber)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(newSubscriber.Data.Email)
}

Update a subscriber

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	subscriber := &mailerlite.UpdateSubscriber{
		ID: "1",
		Email: "example@example.com",
		Fields: map[string]interface{}{
			"company": "MailerLite",
		},
	}

	newSubscriber, _, err := client.Subscriber.Update(ctx, subscriber)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(newSubscriber.Data.Email)
}

Delete a subscriber

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Subscriber.Delete(ctx, "subscriber-id")
	if err != nil {
		log.Fatal(err)
	}

	log.Print("Subscriber Deleted")
}

Fetch subscriber activity

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListActivityOptions{
		SubscriberID: "subscriber-id",
		Filters: &[]mailerlite.Filter{{
			Name:  "log_name",
			Value: "email_open",
		}},
		Page:  1,
		Limit: 25,
	}

	_, _, err := client.Subscriber.ActivityLog(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Get single import

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	importReport, _, err := client.Subscriber.GetImport(ctx, "import-id")
	if err != nil {
		log.Fatal(err)
	}

	log.Print(importReport.Data.Done)
}

Groups

Get a list of groups

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListGroupOptions{
		Page:  1,
		Limit: 10,
		Sort: mailerlite.SortByName,
	}

	groups, _, err := client.Group.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(groups.Meta.Total)
}

Create a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Group.Create(ctx, "group-name")
	if err != nil {
		log.Fatal(err)
	}
}

Update a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Group.Update(ctx, "group-id", "Group Name")
	if err != nil {
		log.Fatal(err)
	}
}

Delete a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Group.Delete(ctx, "69861610909337422")
	if err != nil {
		log.Fatal(err)
	}
}

Get subscribers belonging to a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listSubscriberOptions := &mailerlite.ListGroupSubscriberOptions{
		GroupID: "group-id",
		Filters: &[]mailerlite.Filter{{
			Name:  "status",
			Value: "active",
		}},
	}
		
	subscribers, _, err := client.Group.Subscribers(ctx, listSubscriberOptions)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(subscribers.Meta.Total)
}

Assign subscriber to a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Group.Assign(ctx, "group-id", "subscriber-id")
	if err != nil {
		log.Fatal(err)
	}
}

Unassign subscriber from a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Group.UnAssign(ctx, "group-id", "subscriber-id")
	if err != nil {
		log.Fatal(err)
	}
}

Import subscribers to a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.ImportSubscribersOptions{
		Subscribers: []mailerlite.ImportSubscriber{
			{
				Email: "example@example.com",
				Fields: map[string]interface{}{
					"name": "Example",
				},
			},
			{
				Email: "another@example.com",
			},
		},
	}

	_, _, err := client.Group.ImportSubscribers(ctx, "group-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Segments

Get a list of segments

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListSegmentOptions{
		Page:  1,
		Limit: 10,
	}

	_, _, err := client.Segment.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Update a segment

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Segment.Update(ctx, "segment-id", "Segment Name")
	if err != nil {
		log.Fatal(err)
	}
}

Update a segment name and filter

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.UpdateSegmentOptions{
		Name: "Segment Name",
	}

	_, _, err := client.Segment.UpdateWithOptions(ctx, "segment-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Delete a segment

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Segment.Delete(ctx, "segment-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get subscribers belonging to a segment

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listSubscriberOptions := &mailerlite.ListSegmentSubscriberOptions{
		SegmentID: "segment-id",
		Filters: &[]mailerlite.Filter{{
			Name:  "status",
			Value: "active",
		}},
	}
	
	_, _, err := client.Segment.Subscribers(ctx, listSubscriberOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Fields

Get a list of fields

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListFieldOptions{
		Page:   1,
		Limit:  10,
		Filters: &[]mailerlite.Filter{{
			Name:  "keyword",
			Value: "name",
		}},
		Sort:   mailerlite.SortByName,
	}
	
	_, _, err := client.Field.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Create a field

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	// text, number or data
	_, _, err := client.Field.Create(ctx, "field-name", "field-type")
	if err != nil {
		log.Fatal(err)
	}
}

Update a field

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Field.Update(ctx, "field-id", "Field name")
	if err != nil {
		log.Fatal(err)
	}
}

Delete a field

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Field.Delete(ctx, "field-id")
	if err != nil {
		log.Fatal(err)
	}
}

Automations

Get a list of automations

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListAutomationOptions{
		Filters: &[]mailerlite.Filter{{
			Name:  "status",
			Value: true,
		}},
		Page:  1,
		Limit: 10,
	}
	
	_, _, err := client.Automation.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.Get(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get subscribers activity for an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListAutomationSubscriberOptions{
		Filters: &[]mailerlite.Filter{{
			Name:  "status",
			Value: "active",
		}},
		AutomationID: "automation-id",
		Page:         1,
		Limit:        10,
	}

	_, _, err := client.Automation.Subscribers(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Create a draft automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.Create(ctx, "Automation Name")
	if err != nil {
		log.Fatal(err)
	}
}

Delete an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Automation.Delete(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Update an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.UpdateAutomationOptions{
		Name: "Automation Name",
	}

	_, _, err := client.Automation.Update(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Copy an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.Copy(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Enable an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.Enable(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Disable an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.Disable(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Send a test of an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.TestAutomationOptions{
		Email: "test@example.com",
	}

	_, err := client.Automation.Test(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Add subscribers to an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AddSubscribersToAutomationOptions{
		Emails: []string{"test@example.com"},
	}

	_, err := client.Automation.AddSubscribers(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Re-add subscribers to an automation

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.ReaddAutomationSubscribersOptions{
		ReaddSubscribersTo: "last_failed_step",
		Filter: mailerlite.AutomationActivityFilter{
			Status: "failed",
		},
	}

	_, err := client.Automation.Readd(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Copy automation activity subscribers to a group

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AutomationActivityAddToGroupOptions{
		DestinationGroups: []string{"group-id"},
		Filter: mailerlite.AutomationActivityFilter{
			Status: "completed",
		},
	}

	_, err := client.Automation.ActivityAddToGroup(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Cancel active automation runs

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AutomationActivityOptions{
		Filter: mailerlite.AutomationActivityFilter{
			Status: "active",
		},
	}

	_, err := client.Automation.ActivityCancelAutomation(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Skip the delay of active automation runs

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AutomationActivityOptions{
		Filter: mailerlite.AutomationActivityFilter{
			Status: "active",
		},
	}

	_, err := client.Automation.ActivitySkipDelay(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Get a list of automation triggers

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.ListTriggers(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Create an automation trigger

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.CreateAutomationTriggerOptions{
		Type: mailerlite.AutomationTriggerTypeSubscriberJoinsGroup,
		Data: map[string]interface{}{
			"group_ids": []string{"group-id"},
		},
	}

	_, _, err := client.Automation.CreateTrigger(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation trigger

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.GetTrigger(ctx, "automation-id", "trigger-id")
	if err != nil {
		log.Fatal(err)
	}
}

Update an automation trigger

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.UpdateAutomationTriggerOptions{
		Data: map[string]interface{}{
			"group_ids": []string{"group-id"},
		},
	}

	_, _, err := client.Automation.UpdateTrigger(ctx, "automation-id", "trigger-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Delete an automation trigger

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Automation.DeleteTrigger(ctx, "automation-id", "trigger-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get a list of automation steps

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.ListSteps(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Create an automation step

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.CreateAutomationStepOptions{
		Type:     mailerlite.AutomationStepTypeDelay,
		ParentID: "parent-step-id",
		Data: map[string]interface{}{
			"unit":  "days",
			"value": 1,
		},
	}

	_, _, err := client.Automation.CreateStep(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation step

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.GetStep(ctx, "automation-id", "step-id")
	if err != nil {
		log.Fatal(err)
	}
}

Update an automation step

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.UpdateAutomationStepOptions{
		Data: map[string]interface{}{
			"unit":  "days",
			"value": 2,
		},
	}

	_, _, err := client.Automation.UpdateStep(ctx, "automation-id", "step-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Delete an automation step

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.DeleteAutomationStepOptions{
		BranchToDelete: mailerlite.AutomationBranchAll,
	}

	_, err := client.Automation.DeleteStep(ctx, "automation-id", "step-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Copy an automation step

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.CopyAutomationStepOptions{
		ParentID: "parent-step-id",
	}

	_, _, err := client.Automation.CopyStep(ctx, "automation-id", "step-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Reorder automation steps

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.ReorderAutomationStepsOptions{
		DraggedStepID:   "step-id",
		NewParentStepID: "parent-step-id",
	}

	_, _, err := client.Automation.ReorderSteps(ctx, "automation-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Select an automation A/B test winner

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.SelectAbTestWinnerOptions{
		AbTestWinner: mailerlite.AutomationBranchAPath,
	}

	_, _, err := client.Automation.SelectAbTestWinner(ctx, "automation-id", "step-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation email content

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.GetEmailContent(ctx, "automation-id", "email-id")
	if err != nil {
		log.Fatal(err)
	}
}

Update an automation email content

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.UpdateAutomationEmailContentOptions{
		HTML: "<p>Hello</p>",
	}

	_, _, err := client.Automation.UpdateEmailContent(ctx, "automation-id", "email-id", options)
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation email report stats

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AutomationEmailStatsOptions{
		AutomationID: "automation-id",
		EmailID:      "email-id",
		StartDate:    "2026-01-01",
		EndDate:      "2026-01-31",
	}

	_, _, err := client.Automation.EmailReportStats(ctx, options)
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation email e-commerce stats

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AutomationEmailStatsOptions{
		AutomationID: "automation-id",
		EmailID:      "email-id",
	}

	_, _, err := client.Automation.EmailEcommerceOverviewStats(ctx, options)
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation email unsubscribe reasons

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.EmailUnsubscribeReasons(ctx, "automation-id", "email-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get a list of automation templates

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListAutomationTemplateOptions{
		Filters: &[]mailerlite.Filter{{
			Name:  "type",
			Value: "welcome",
		}},
		Page:  1,
		Limit: 10,
	}

	_, _, err := client.Automation.ListTemplates(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Create an automation from a template

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.SelectTemplate(ctx, "template-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get automation step run counts

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.StepRunCounts(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation step run count

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.StepRunCount(ctx, "automation-id", "step-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get automation trigger run counts

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.TriggerRunCounts(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation trigger run count

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.TriggerRunCount(ctx, "automation-id", "trigger-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get automation A/B test stats

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.AbStepsStats(ctx, "automation-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get an automation A/B test step stats

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Automation.AbStepStats(ctx, "automation-id", "step-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get subscribers of an automation step

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListAutomationStepSubscriberOptions{
		AutomationID: "automation-id",
		StepID:       "step-id",
		Status:       "queued",
		Page:         1,
		Limit:        10,
	}

	_, _, err := client.Automation.StepSubscribers(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Get subscribers of an automation trigger

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListAutomationTriggerSubscriberOptions{
		AutomationID: "automation-id",
		TriggerID:    "trigger-id",
		Status:       "completed",
		Page:         1,
		Limit:        10,
	}

	_, _, err := client.Automation.TriggerSubscribers(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Get automation order activity

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListAutomationOrderActivityOptions{
		AutomationID: "automation-id",
		TimeFrame:    "month",
		Page:         1,
		Limit:        10,
	}

	_, _, err := client.Automation.OrderActivity(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Get automation email totals

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AutomationStatsOptions{
		AutomationID: "automation-id",
	}

	_, _, err := client.Automation.MonthlyEmailTotals(ctx, options)
	if err != nil {
		log.Fatal(err)
	}
}

Get automation e-commerce totals

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.AutomationStatsOptions{
		AutomationID: "automation-id",
	}

	_, _, err := client.Automation.MonthlyEcommerceTotals(ctx, options)
	if err != nil {
		log.Fatal(err)
	}
}

Campaigns

Get a list of campaigns

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListCampaignOptions{
		Filters: &[]mailerlite.Filter{{
			Name:  "status",
			Value: "draft",
		}},
		Page:  1,
		Limit: 10,
	}
	
	_, _, err := client.Campaign.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Get a campaign

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Campaign.Get(ctx, "campaign-id")
	if err != nil {
		log.Fatal(err)
	}
}

Create a campaign

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	emails := &[]mailerlite.Emails{
		{
			Subject:  "Subject",
			FromName: "Your Name",
			From:     "your@domain.com",
			Content:  "<p>This is the HTML content</p>",
		},
	}
	
	campaign := &mailerlite.CreateCampaign{
		Name:   "Campaign Name",
		Type:   mailerlite.CampaignTypeRegular,
		Emails: *emails,
	}
	
	_, _, err := client.Campaign.Create(ctx, campaign)
	if err != nil {
		log.Fatal(err)
	}
}

Update a campaign

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	emails := &[]mailerlite.Emails{
		{
			Subject:  "Subject",
			FromName: "Your Name",
			From:     "your@domain.com",
			Content:  "<p>This is the HTML content</p>",
		},
	}
	
	campaign := &mailerlite.UpdateCampaign{
		Name:   "Campaign Name",
		Type:   mailerlite.CampaignTypeRegular,
		Emails: *emails,
	}
	
	_, _, err := client.Campaign.Update(ctx, "campaign-id", campaign)
	if err != nil {
		log.Fatal(err)
	}
}

Schedule a campaign

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	schedule := &mailerlite.ScheduleCampaign{
		Delivery: mailerlite.CampaignScheduleTypeInstant,
	}
	
	_, _, err := client.Campaign.Schedule(ctx, "campaign-id", schedule)
	if err != nil {
		log.Fatal(err)
	}
}

Cancel a ready campaign

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Campaign.Cancel(ctx, "campaign-id")
	if err != nil {
		log.Fatal(err)
	}
}

Delete a campaign

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Campaign.Delete(ctx, "campaign-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get subscribers activity for a campaign

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListCampaignSubscriberOptions{
		CampaignID: "campaign-id",
		Filters: &[]mailerlite.Filter{
			{
				Name:  "type",
				Value: "opened",
			},
			{
				Name:  "search",
				Value: "subscriber@example.com",
			},
		},
		Page:  1,
		Limit: 10,
	}

	_, _, err := client.Campaign.Subscribers(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Forms

Get a list of forms

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListFormOptions{
		Type:   mailerlite.FormTypePopup,
		Page:   1,
		Limit:  10,
      	Filters: &[]mailerlite.Filter{{
      		Name:  "name",
      		Value: "Form Name",
      	}},
		Sort:   mailerlite.SortByName,
	}
	
	_, _, err := client.Form.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Get a form

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	form, _, err := client.Form.Get(ctx, "form-id")
	if err != nil {
		log.Fatal(err)
	}
}

Update a form

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Form.Update(ctx, "form-id", "Form Name")
	if err != nil {
		log.Fatal(err)
	}
}

Delete a form

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Form.Delete(ctx, "form-id")
	if err != nil {
		log.Fatal(err)
	}
}

Get subscribers of a form

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListFormSubscriberOptions{
		Page:  1,
		Limit: 10,
		Filters: &[]mailerlite.Filter{{
			Name:  "status",
			Value: "active",
		}},
	}

	_, _, err := client.Form.Subscribers(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}
}

Batching

Create a new batch

TBC

Webhooks

Get a list of webhooks

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.ListWebhookOptions{
		Sort:  mailerlite.SortByName,
		Page:  1,
		Limit: 10,
	}

	_, _, err := client.Webhook.List(ctx, options)
	if err != nil {
		log.Fatal(err)
	}
}

Get a webhook

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Webhook.Get(ctx, "webhook-id")
	if err != nil {
		log.Fatal(err)
	}
}

Create a webhook

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.CreateWebhookOptions{
		Name:   "",
		Events: []string{"subscriber.bounced"},
		Url:    "https://example.com/webhook",
	}
	
	_, _, err := client.Webhook.Create(ctx, options)
	if err != nil {
		log.Fatal(err)
	}
}

Update a webhook

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	options := &mailerlite.UpdateWebhookOptions{
		WebhookID: "webhook-id",
		Events:    []string{"subscriber.bounced", "subscriber.unsubscribed"},
		Name:      "Update",
	}
	
	_, _, err := client.Webhook.Update(ctx, options)
	if err != nil {
		log.Fatal(err)
	}
}

Delete a webhook

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, err := client.Webhook.Delete(ctx, "75000728688526795")
	if err != nil {
		log.Fatal(err)
	}
}

Timezones

Get a list of timezones

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Timezone.List(ctx)
	if err != nil {
		log.Fatal(err)
	}
}

Campaign languages

Get a list of languages

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	_, _, err := client.Campaign.Languages(ctx)
	if err != nil {
		log.Fatal(err)
	}
}

Emails

These endpoints work on emails built with the simple editor. Get the email ID from a campaign's emails array.

To get a simple editor email, create a campaign with Editor: mailerlite.EmailEditorSimple. You can set Editor only when you create the campaign. Do not set Content. If the email does not use the simple editor, the API returns a 422 error.

TemplateID is optional, and works only with Editor: mailerlite.EmailEditorSimple. Use a simple editor template from the shared gallery, or a template that you saved from your own email with client.Email.CreateTemplate. The API copies the template into the new email. Without TemplateID, the email starts from the default blank template. Use the same Editor and the same TemplateID on every email of the campaign.

Every write must send the etag that the last read or write of the email returned. If the etag is empty, the API returns a 422 error. If the email changed after that read or write, the API returns a 409 error with the code mailerlite.ErrorCodeVersionConflict. The Etag field of the *mailerlite.ErrorResponse contains the current etag of the email.

If an email, block or version ID is empty, the SDK returns mailerlite.ErrEmptyID and sends no request. If an ID contains a character that is not a digit, the SDK returns mailerlite.ErrInvalidID and sends no request.

The SDK does not check the other values, for example the placement. It sends them as you give them. The API checks them and returns an error. The validate methods return Valid: false with the errors.

Create a campaign with the simple editor

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	campaign := &mailerlite.CreateCampaign{
		Name: "Campaign Name",
		Type: mailerlite.CampaignTypeRegular,
		Emails: []mailerlite.Emails{
			{
				Subject:  "Subject",
				FromName: "Your Name",
				From:     "your@domain.com",
				Editor:   mailerlite.EmailEditorSimple,
			},
		},
	}

	root, _, err := client.Campaign.Create(ctx, campaign)
	if err != nil {
		log.Fatal(err)
	}

	email, _, err := client.Email.Get(ctx, root.Data.Emails[0].ID)
	if err != nil {
		log.Fatal(err)
	}

	log.Print(email.Etag)
}

Get an email

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	email, _, err := client.Email.Get(ctx, "email-id")
	if err != nil {
		log.Fatal(err)
	}

	log.Print(email.Etag)
}

Get the HTML of an email

The HTML is a preview. Do not send it yourself, because it contains personalization tokens and no tracking URLs.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	html, _, err := client.Email.GetHTML(ctx, "email-id")
	if err != nil {
		log.Fatal(err)
	}

	log.Print(html.Data.HTML)
}

Get a block of an email

Placement gives the IDs of the blocks before and after the block.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	block, _, err := client.Email.GetBlock(ctx, "email-id", "block-id")
	if err != nil {
		log.Fatal(err)
	}

	log.Print(block.Data.Type)
}

Insert a block into an email

Without Placement, the block goes at the end of the email. EditorContent is required. Use an empty map for a block with no content, such as a divider.

If the API removes attributes or mobile keys that the block type does not declare, the response has Rejected with the keys of the removed values. The API saves the rest of the block.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	email, _, err := client.Email.Get(ctx, "email-id")
	if err != nil {
		log.Fatal(err)
	}

	block := &mailerlite.CreateEmailBlockOptions{
		Type: mailerlite.EmailBlockTypeParagraph,
		EditorContent: map[string]interface{}{
			"content": []interface{}{
				map[string]interface{}{"type": "text", "text": "Hello"},
			},
		},
		Placement: &mailerlite.EmailBlockPosition{
			Position: mailerlite.EmailBlockPositionAfter,
			BlockID:  "block-id",
		},
		Etag: email.Etag,
	}

	created, _, err := client.Email.CreateBlock(ctx, "email-id", block)
	if err != nil {
		log.Fatal(err)
	}

	// Use created.Etag for the next write to this email.
	log.Print(created.Data.ID)
}

Update the content of a block

EditorContent replaces the whole content of the block. Get the block first, change it, and send all of it back.

If the API removes attributes or mobile keys that the block type does not declare, the response has Rejected with the keys of the removed values.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	block, _, err := client.Email.GetBlock(ctx, "email-id", "block-id")
	if err != nil {
		log.Fatal(err)
	}

	content := block.Data.EditorContent
	content["attrs"] = map[string]interface{}{"textAlign": "center"}

	updated, _, err := client.Email.UpdateBlock(ctx, "email-id", "block-id", &mailerlite.UpdateEmailBlockOptions{
		EditorContent: content,
		Etag:          block.Etag,
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Print(updated.Etag)
}

Move a block

The position is start, end, before or after. BlockID is required for before and after, and must be a sibling of the block.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	moved, _, err := client.Email.MoveBlock(ctx, "email-id", "block-id", &mailerlite.MoveEmailBlockOptions{
		Placement: mailerlite.EmailBlockPosition{
			Position: mailerlite.EmailBlockPositionBefore,
			BlockID:  "other-block-id",
		},
		Etag: "etag-from-the-last-read-or-write",
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Print(moved.Etag)
}

Delete a block

The blocks nested in the block are deleted too. The response has only the new etag of the email.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	deleted, _, err := client.Email.DeleteBlock(ctx, "email-id", "block-id", "etag-from-the-last-read-or-write")
	if err != nil {
		log.Fatal(err)
	}

	log.Print(deleted.Etag)
}

Validate a block

This checks a block against the same rules as an insert, and saves nothing. Invalid content is a successful request, so check Valid and Errors.

If the content is valid but an insert would remove some keys, the result has Rejected with these keys.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	result, _, err := client.Email.ValidateBlock(ctx, &mailerlite.CreateEmailBlockOptions{
		Type: mailerlite.EmailBlockTypeParagraph,
		EditorContent: map[string]interface{}{
			"attrs": map[string]interface{}{"textAlign": "center"},
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	if !result.Valid {
		log.Print(result.Errors)
	}
}

Update the design settings of an email

Set only the keys to change. The API stores every value that you send on the email, also a value that is the same as the account value.

  • To unset a brand colour, send an empty string. The API stores the value empty, and the email then uses the account value.
  • To unset a footer text, send an empty editor document: an empty EditorContent map, or a map with an empty content list.
  • Other colours, sizes, Button.BorderRadius and Brand.Style cannot be empty. An empty string gives a 422 error.

If the API removes an unsafe value, the response has Rejected with the keys of the removed values.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	updated, _, err := client.Email.UpdateSettings(ctx, "email-id", &mailerlite.UpdateEmailSettingsOptions{
		EmailSettings: mailerlite.EmailSettings{
			Brand: &mailerlite.EmailBrandSettings{
				Mode:            mailerlite.String(mailerlite.EmailBrandModeCustom),
				BackgroundColor: mailerlite.String("#f4f4f4"),
			},
		},
		Etag: "etag-from-the-last-read-or-write",
	})
	if err != nil {
		log.Fatal(err)
	}

	if updated.Rejected != nil {
		log.Print(updated.Rejected.Keys)
	}
}

Validate the design settings of an email

This checks a settings change against the same rules as an update, and saves nothing. No etag is necessary.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	result, _, err := client.Email.ValidateSettings(ctx, "email-id", &mailerlite.UpdateEmailSettingsOptions{
		EmailSettings: mailerlite.EmailSettings{
			Button: &mailerlite.EmailButtonSettings{BorderRadius: mailerlite.String("12")},
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	if !result.Valid {
		log.Print(result.Errors)
	}
}

Save an email as a template

To start a new campaign from the saved template, convert template.Data.ID to an int with strconv.Atoi, and send it as TemplateID when you create the campaign.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	template, _, err := client.Email.CreateTemplate(ctx, "email-id", &mailerlite.CreateEmailTemplateOptions{
		Name: "Newsletter",
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Print(template.Data.ID)
}

Get a list of versions of an email

The list is newest first. Current marks the version that the email is on now.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	versions, _, err := client.Email.ListVersions(ctx, "email-id", &mailerlite.ListEmailVersionOptions{
		Page:  1,
		Limit: 10,
	})
	if err != nil {
		log.Fatal(err)
	}

	log.Print(len(versions.Data))
}

Save a version of an email

Changes made through the API do not save a version. If nothing changed since the last version, the API returns that version with status 200 instead of 201.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	version, _, err := client.Email.CreateVersion(ctx, "email-id")
	if err != nil {
		log.Fatal(err)
	}

	log.Print(version.Data.ID)
}

Restore a version of an email

The API saves the replaced state as a version first, so you can undo a restore. The response is the restored email.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	email, _, err := client.Email.RestoreVersion(ctx, "email-id", "version-id", "etag-from-the-last-read-or-write")
	if err != nil {
		log.Fatal(err)
	}

	log.Print(email.Etag)
}

Retry a write with an idempotency key

If a write fails with a network error, you do not know if the API did the write. Send the write with an idempotency key, and retry it with the same key and the same request. If the first request succeeded, the API returns the stored response again and does not do the write two times.

  • Use a new key for each write. The API keeps a key for 24 hours.
  • If the key is empty, the SDK sends no Idempotency-Key header.
  • The SDK does not check the format of the key. The API returns a 400 error for an invalid key.
  • net/http returns an error for a key with a control character, for example a new line. It then sends no request.
  • For HTTP/1.1, net/http removes spaces at the start and the end of the key.
  • The same key with a different request returns a 422 error. If the first request is still running, the API returns a 409 error without a code.
  • The API uses the key only for the POST, PATCH and DELETE requests of client.Email, except the validate methods.
package main

import (
	"context"
	"errors"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	block := &mailerlite.CreateEmailBlockOptions{
		Type:          mailerlite.EmailBlockTypeDivider,
		EditorContent: map[string]interface{}{},
		Etag:          "etag-from-the-last-read-or-write",
	}

	keyCtx := mailerlite.WithIdempotencyKey(ctx, "insert-divider-0001")

	created, res, err := client.Email.CreateBlock(keyCtx, "email-id", block)
	var errResp *mailerlite.ErrorResponse
	if err != nil && !errors.As(err, &errResp) {
		// Network error: retry with the same key and the same request.
		created, res, err = client.Email.CreateBlock(keyCtx, "email-id", block)
	}
	if err != nil {
		log.Fatal(err)
	}

	log.Print(created.Data.ID, res.IsIdempotentReplay())
}

Brand styles

Get the brand styles

The response has the account palette (Brand, nil if the account has none), the default palette and the presets. To apply a preset to an email, send its Identifier as Brand.Style in Email.UpdateSettings.

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	styles, _, err := client.BrandStyle.Get(ctx)
	if err != nil {
		log.Fatal(err)
	}

	for _, preset := range styles.Data.Presets {
		log.Print(preset.Identifier)
	}
}

Files

Get a list of files

Use a file URL to put an image into an email block. You can filter by name (partial match) and type (image, pdf or epub).

package main

import (
	"context"
	"log"

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

var APIToken = "Api Token Here"

func main() {
	client := mailerlite.NewClient(APIToken)

	ctx := context.TODO()

	listOptions := &mailerlite.ListFileOptions{
		Filters: &[]mailerlite.Filter{{
			Name:  "type",
			Value: mailerlite.FileTypeImage,
		}},
		Sort:  mailerlite.SortByCreatedAtDescending,
		Page:  1,
		Limit: 10,
	}

	files, _, err := client.File.List(ctx, listOptions)
	if err != nil {
		log.Fatal(err)
	}

	for _, file := range files.Data {
		log.Print(file.URL)
	}
}

Testing

We provide interfaces for all services to help with testing

type mockSubscriberService struct {
	mailerlite.SubscriberService
}

func (m *mockSubscriberService) List(ctx context.Context, options *mailerlite.ListSubscriberOptions) (*mailerlite.RootSubscribers, *mailerlite.Response, error) {
	return &mailerlite.RootSubscribers{Data: []mailerlite.Subscriber{{Email: "example@example.com"}}}, nil, nil
}

func TestListSubscribers(t *testing.T) {
	client := &mailerlite.Client{}
	client.Subscriber = &mockSubscriberService{}

	ctx := context.Background()
	result, _, err := client.Subscriber.List(ctx, nil)
	if err != nil || len(result.Data) == 0 || result.Data[0].Email != "example@example.com" {
		t.Fatalf("mock failed")
	}
}

pkg/testing

$ go test

Support and Feedback

In case you find any bugs, submit an issue directly here in GitHub.

You are welcome to create SDK for any other programming language.

If you have any trouble using our API or SDK feel free to contact our support by email info@mailerlite.com

The official API documentation is at https://developers.mailerlite.com

License

The MIT License (MIT)

About

Go SDK for MailerLite

Topics

Resources

Stars

6 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages