Hey there, fellow developer! Ready to supercharge your CRM game with Nutshell? Let's dive into building a slick API integration using Go. Nutshell's API is a powerhouse for managing contacts, leads, and more. We'll get you up and running in no time!
Before we jump in, make sure you've got:
Let's kick things off:
mkdir nutshell-integration cd nutshell-integration go mod init github.com/yourusername/nutshell-integration
Now, let's grab the essentials:
go get github.com/go-resty/resty/v2
Time to get cozy with Nutshell. We'll create a reusable client:
package main import ( "github.com/go-resty/resty/v2" ) type NutshellClient struct { client *resty.Client apiKey string } func NewNutshellClient(apiKey string) *NutshellClient { return &NutshellClient{ client: resty.New(), apiKey: apiKey, } }
Let's fetch some contacts, shall we?
func (nc *NutshellClient) GetContacts() ([]Contact, error) { var result struct { Contacts []Contact `json:"contacts"` } _, err := nc.client.R(). SetHeader("Authorization", "Basic "+nc.apiKey). SetResult(&result). Get("https://app.nutshell.com/api/v1/contacts") return result.Contacts, err }
Creating a lead? Easy peasy:
func (nc *NutshellClient) CreateLead(lead Lead) (Lead, error) { var result struct { Lead Lead `json:"lead"` } _, err := nc.client.R(). SetHeader("Authorization", "Basic "+nc.apiKey). SetBody(lead). SetResult(&result). Post("https://app.nutshell.com/api/v1/leads") return result.Lead, err }
Go's got your back with built-in JSON parsing:
type Contact struct { ID int `json:"id"` Name string `json:"name"` // Add more fields as needed }
Don't forget to check for errors:
contacts, err := client.GetContacts() if err != nil { log.Fatalf("Error fetching contacts: %v", err) }
Pagination? We've got you covered:
func (nc *NutshellClient) GetContactsPaginated(page, limit int) ([]Contact, error) { // Implementation here }
Let's wrap it all up in a neat CLI package:
package main import ( "flag" "fmt" "log" ) func main() { apiKey := flag.String("api-key", "", "Nutshell API key") flag.Parse() if *apiKey == "" { log.Fatal("API key is required") } client := NewNutshellClient(*apiKey) contacts, err := client.GetContacts() if err != nil { log.Fatalf("Error fetching contacts: %v", err) } for _, contact := range contacts { fmt.Printf("Contact: %s (ID: %d)\n", contact.Name, contact.ID) } }
Don't forget to test! Here's a quick example:
func TestGetContacts(t *testing.T) { // Mock the API response // Test the GetContacts function // Assert the results }
And there you have it! You've just built a solid foundation for your Nutshell API integration in Go. The sky's the limit from here – add more endpoints, build a full-fledged application, or integrate it into your existing systems.
Now go forth and conquer the world of CRM integrations! Happy coding!