Hey there, fellow Go enthusiast! Ready to supercharge your CRM game with Pipedrive? Let's dive into building a robust API integration that'll make your sales team jump for joy. Pipedrive's API is a powerhouse, and when combined with Go's efficiency, you're in for a treat.
Before we roll up our sleeves, make sure you've got:
Let's kick things off:
mkdir pipedrive-integration cd pipedrive-integration go mod init pipedrive-integration
We'll need a HTTP client. Go's standard library has us covered, but let's add a helper:
go get github.com/go-resty/resty/v2
Pipedrive uses API key auth. Let's set it up:
import "github.com/go-resty/resty/v2" client := resty.New() client.SetHeader("Authorization", "Bearer YOUR_API_KEY")
Time to make our first request! Let's fetch some deals:
resp, err := client.R(). SetQueryParam("limit", "10"). Get("https://api.pipedrive.com/v1/deals") if err != nil { panic(err) } fmt.Println(resp.String())
We've already seen how to fetch deals. Let's wrap it in a function:
func getDeals(client *resty.Client) ([]Deal, error) { var result struct { Data []Deal `json:"data"` } _, err := client.R(). SetQueryParam("limit", "10"). SetResult(&result). Get("https://api.pipedrive.com/v1/deals") return result.Data, err }
func createContact(client *resty.Client, name, email string) error { _, err := client.R(). SetBody(map[string]string{ "name": name, "email": email, }). Post("https://api.pipedrive.com/v1/persons") return err }
func updateDeal(client *resty.Client, dealID int, status string) error { _, err := client.R(). SetBody(map[string]string{ "status": status, }). Put(fmt.Sprintf("https://api.pipedrive.com/v1/deals/%d", dealID)) return err }
Pipedrive returns errors in a specific format. Let's handle them:
type PipedriveError struct { Success bool `json:"success"` Error string `json:"error"` } func handlePipedriveError(resp *resty.Response) error { if resp.IsError() { var pErr PipedriveError if err := json.Unmarshal(resp.Body(), &pErr); err != nil { return err } return fmt.Errorf("Pipedrive error: %s", pErr.Error) } return nil }
Pipedrive has rate limits, so let's be good citizens:
time.Sleep(200 * time.Millisecond)
For pagination, use the start
parameter:
func getAllDeals(client *resty.Client) ([]Deal, error) { var allDeals []Deal start := 0 for { var result struct { Data []Deal `json:"data"` AdditionalData struct { Pagination struct { MoreItemsInCollection bool `json:"more_items_in_collection"` } } `json:"additional_data"` } _, err := client.R(). SetQueryParam("start", strconv.Itoa(start)). SetQueryParam("limit", "100"). SetResult(&result). Get("https://api.pipedrive.com/v1/deals") if err != nil { return nil, err } allDeals = append(allDeals, result.Data...) if !result.AdditionalData.Pagination.MoreItemsInCollection { break } start += 100 time.Sleep(200 * time.Millisecond) } return allDeals, nil }
Don't forget to test! Here's a simple example:
func TestGetDeals(t *testing.T) { client := resty.New() client.SetHeader("Authorization", "Bearer YOUR_API_KEY") deals, err := getDeals(client) if err != nil { t.Fatalf("Failed to get deals: %v", err) } if len(deals) == 0 { t.Fatal("No deals returned") } }
And there you have it! You've just built a solid Pipedrive API integration in Go. Remember, this is just the beginning. Explore more endpoints, add more features, and most importantly, have fun coding!
Keep rocking, Gopher! 🚀