mailerlite / mailerlite-go

Go SDK for MailerLite
MIT License
5 stars 4 forks source link
golang sdk

MailerLite Golang SDK

MIT licensed Go Reference

Table of Contents

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(subscribers.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 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.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)
}

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

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")
}

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

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

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

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",
        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)
    }
}

Testing

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)