MailerLite Golang SDK
- Installation
- Usage
We recommend using this package with golang modules
$ go get github.com/mailerlite/mailerlite-go
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)
}
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)
}
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)
}
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.Subscriber{
Email: "example@example.com",
Fields: map[string]interface{}{
"city": "Vilnius",
},
}
newSubscriber, _, err := client.Subscriber.Create(ctx, subscriber)
if err != nil {
log.Fatal(err)
}
log.Print(newSubscriber.Data.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()
subscriber := &mailerlite.Subscriber{
Email: "example@example.com",
Fields: map[string]interface{}{
"company": "MailerLite",
},
}
newSubscriber, _, err := client.Subscriber.Create(ctx, subscriber)
if err != nil {
log.Fatal(err)
}
log.Print(newSubscriber.Data.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()
_, err := client.Subscriber.Delete(ctx, "subscriber-id")
if err != nil {
log.Fatal(err)
}
log.Print("Subscriber Deleted")
}
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)
}
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)
}
}
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)
}
}
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)
}
}
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)
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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",
Page: 1,
Limit: 10,
}
_, _, err := client.Campaign.Subscribers(ctx, listOptions)
if err != nil {
log.Fatal(err)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
TBC
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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)
}
}
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")
}
}
$ go test
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