modulepackage
1.0.5
Repository: https://github.com/mailerlite/mailerlite-go.git
Documentation: pkg.go.dev
# README
MailerLite Golang SDK
Table of Contents
- Installation
- 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: "[email protected]"
}
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: "[email protected]",
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: "[email protected]",
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: "[email protected]",
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: "[email protected]",
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
$ 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 [email protected]
The official API documentation is at https://developers.mailerlite.com
License
# Functions
Bool is a helper routine that allocates a new bool value to store v and returns a pointer to it.
Int is a helper routine that allocates a new int value to store v and returns a pointer to it.
Int64 is a helper routine that allocates a new int64 value to store v and returns a pointer to it.
NewClient - creates a new client instance.
NewFilter returns a new filter initialized with the given name and value.
String is a helper routine that allocates a new string value to store v and returns a pointer to it.
# Constants
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Variables
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Client - base api client.
No description provided by the author
No description provided by the author
No description provided by the author
CreateCampaign - modifies the behavior of CampaignService.Create method.
CreateWebhookOptions - modifies the behavior of WebhookService.Create method.
No description provided by the author
No description provided by the author
ErrorResponse is a MailerLite API error response.
No description provided by the author
No description provided by the author
Filter is one of the arguments which has a name and a value.
No description provided by the author
GetCampaignOptions - modifies the behavior of CampaignService.Get method.
GetSubscriberOptions - modifies the behavior of SubscriberService.Get method.
No description provided by the author
No description provided by the author
Links manages links that are returned along with a List.
ListAutomationOptions - modifies the behavior of AutomationService.List method.
ListAutomationSubscriberOptions - modifies the behavior of AutomationService.Subscribers method.
ListCampaignOptions - modifies the behavior of CampaignService.List method.
No description provided by the author
ListFieldOptions - modifies the behavior of FieldService.List method.
ListFormOptions - modifies the behavior of FormService.List method.
ListFormSubscriberOptions - modifies the behavior of FormService.Subscribers method.
ListGroupOptions - modifies the behavior of GroupService.List method.
No description provided by the author
ListSegmentOptions - modifies the behavior of SegmentService.List method.
ListSegmentSubscriberOptions - modifies the behavior of SegmentService.Subscribers method.
ListSubscriberOptions - modifies the behavior of SubscriberService.List method.
ListWebhookOptions - modifies the behavior of WebhookService.List method.
No description provided by the author
No description provided by the author
No description provided by the author
Rate represents the rate limit for the current client.
RateLimitError occurs when MailerLite returns 403 Forbidden response with a rate limit remaining value of 0.
No description provided by the author
No description provided by the author
Response is a MailerLite API response.
No description provided by the author
ScheduleCampaign - modifies the behavior of CampaignService.Schedule method.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
UpdateWebhookOptions - modifies the behavior of WebhookService.Create method.
No description provided by the author
# Type aliases
AuthError occurs when using HTTP Authentication fails.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author