Back

Step by Step Guide to Building a lemlist API Integration in Go

Aug 14, 20247 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to supercharge your outreach game with lemlist? Let's dive into building a slick API integration that'll have you managing campaigns and leads like a pro. We'll keep things snappy and focus on the good stuff – no fluff, just pure Gopher goodness.

Prerequisites

Before we jump in, make sure you've got:

  • Go installed (you're a pro, so I'm sure you do)
  • A lemlist API key (if you don't have one, grab it from your account settings)
  • Your favorite code editor at the ready

Setting up the project

Let's kick things off right:

mkdir lemlist-integration cd lemlist-integration go mod init github.com/yourusername/lemlist-integration

Authentication

First things first, let's handle that API key:

const apiKey = "your-api-key-here"

Pro tip: In a real-world scenario, you'd want to use environment variables or a secure config management system. But hey, we're keeping it simple for now!

Making API requests

Time to create our HTTP client. We'll use this bad boy for all our lemlist shenanigans:

package main import ( "net/http" "time" ) func newClient() *http.Client { return &http.Client{ Timeout: time.Second * 10, } }

Implementing key lemlist API endpoints

Let's tackle the big three: Campaigns, Leads, and Activities. We'll create a struct to hold our client and methods for each endpoint:

type LemlistClient struct { client *http.Client apiKey string baseURL string } func NewLemlistClient(apiKey string) *LemlistClient { return &LemlistClient{ client: newClient(), apiKey: apiKey, baseURL: "https://api.lemlist.com/api", } } func (c *LemlistClient) GetCampaigns() ([]Campaign, error) { // Implementation here } func (c *LemlistClient) AddLead(campaignID string, lead Lead) error { // Implementation here } func (c *LemlistClient) GetActivities(campaignID string) ([]Activity, error) { // Implementation here }

Error handling and response parsing

Let's keep it clean with some error handling and JSON parsing:

func (c *LemlistClient) doRequest(req *http.Request, v interface{}) error { req.Header.Set("Authorization", "Bearer "+c.apiKey) resp, err := c.client.Do(req) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return fmt.Errorf("API request failed with status code: %d", resp.StatusCode) } return json.NewDecoder(resp.Body).Decode(v) }

Creating a reusable lemlist client

Now let's put it all together:

func main() { client := NewLemlistClient("your-api-key") campaigns, err := client.GetCampaigns() if err != nil { log.Fatal(err) } for _, campaign := range campaigns { fmt.Printf("Campaign: %s\n", campaign.Name) } }

Example use cases

Here's a quick example of creating a campaign and adding a lead:

func createCampaignExample(client *LemlistClient) { campaign := Campaign{ Name: "Go Developers Outreach", // Add other necessary fields } err := client.CreateCampaign(campaign) if err != nil { log.Fatal(err) } } func addLeadExample(client *LemlistClient, campaignID string) { lead := Lead{ Email: "[email protected]", FirstName: "Gopher", LastName: "GoLang", } err := client.AddLead(campaignID, lead) if err != nil { log.Fatal(err) } }

Testing the integration

Don't forget to test! Here's a quick unit test to get you started:

func TestGetCampaigns(t *testing.T) { client := NewLemlistClient("test-api-key") campaigns, err := client.GetCampaigns() if err != nil { t.Fatalf("Expected no error, got %v", err) } if len(campaigns) == 0 { t.Fatal("Expected campaigns, got none") } }

Best practices and optimization

Remember to implement rate limiting to play nice with lemlist's API. A simple time.Sleep() between requests can do wonders:

time.Sleep(time.Second / 10) // Max 10 requests per second

Conclusion

And there you have it! You've just built a lean, mean lemlist integration machine. Remember, this is just the beginning – there's a whole world of lemlist API endpoints to explore. Keep coding, keep optimizing, and most importantly, keep having fun with Go!

Happy coding, Gophers! 🚀🐹