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.
Before we jump in, make sure you've got:
Let's kick things off right:
mkdir lemlist-integration cd lemlist-integration go mod init github.com/yourusername/lemlist-integration
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!
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, } }
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 }
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) }
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) } }
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) } }
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") } }
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
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! 🚀🐹