Hey there, fellow Go enthusiast! Ready to dive into the world of FreshBooks API integration? You're in for a treat. We'll be using the awesome toggl/go-freshbooks
package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
Got all that? Great! Let's move on.
First things first, let's create a new Go project:
mkdir freshbooks-integration cd freshbooks-integration go mod init freshbooks-integration
Now, let's grab the toggl/go-freshbooks
package:
go get github.com/toggl/go-freshbooks
Alright, time to get those API credentials working for us. Head over to your FreshBooks account and grab your API key and secret. Then, let's initialize the FreshBooks client:
import ( "github.com/toggl/go-freshbooks" ) client := freshbooks.NewClient("YOUR_API_KEY", "YOUR_API_SECRET")
Now that we're all set up, let's dive into some basic operations.
account, err := client.Accounts.Get(context.Background(), "YOUR_ACCOUNT_ID") if err != nil { log.Fatal(err) } fmt.Printf("Account Name: %s\n", account.Name)
clients, err := client.Clients.List(context.Background(), "YOUR_ACCOUNT_ID", nil) if err != nil { log.Fatal(err) } for _, client := range clients.Clients { fmt.Printf("Client Name: %s\n", client.Name) }
invoice := &freshbooks.Invoice{ CustomerID: 12345, Lines: []freshbooks.InvoiceLine{ { Name: "Awesome Service", UnitCost: freshbooks.Amount{Amount: "100.00"}, Quantity: 1, }, }, } createdInvoice, err := client.Invoices.Create(context.Background(), "YOUR_ACCOUNT_ID", invoice) if err != nil { log.Fatal(err) } fmt.Printf("Invoice created with ID: %d\n", createdInvoice.ID)
Ready to level up? Let's tackle some more advanced stuff.
Updating a client:
updatedClient := &freshbooks.Client{ ID: 12345, Name: "Updated Client Name", } client, err := client.Clients.Update(context.Background(), "YOUR_ACCOUNT_ID", updatedClient) if err != nil { log.Fatal(err) }
Deleting an invoice:
err := client.Invoices.Delete(context.Background(), "YOUR_ACCOUNT_ID", 67890) if err != nil { log.Fatal(err) }
options := &freshbooks.ListOptions{ Page: 1, PerPage: 30, } for { clients, err := client.Clients.List(context.Background(), "YOUR_ACCOUNT_ID", options) if err != nil { log.Fatal(err) } for _, client := range clients.Clients { // Process each client } if clients.Pages.Page >= clients.Pages.Pages { break } options.Page++ }
if err != nil { if freshbooksErr, ok := err.(*freshbooks.Error); ok { fmt.Printf("FreshBooks API Error: %s\n", freshbooksErr.Message) if freshbooksErr.StatusCode == 429 { // Handle rate limiting time.Sleep(time.Second * 5) // Retry the request } } else { log.Fatal(err) } }
Here are some tips to keep your code clean and efficient:
Don't forget to test! Here's a quick example of a unit test:
func TestCreateInvoice(t *testing.T) { // Set up mock client mockClient := &MockFreshBooksClient{} // Create a test invoice invoice := &freshbooks.Invoice{ CustomerID: 12345, Lines: []freshbooks.InvoiceLine{ { Name: "Test Service", UnitCost: freshbooks.Amount{Amount: "100.00"}, Quantity: 1, }, }, } // Test the CreateInvoice function createdInvoice, err := CreateInvoice(mockClient, "TEST_ACCOUNT_ID", invoice) assert.NoError(t, err) assert.NotNil(t, createdInvoice) assert.Equal(t, 67890, createdInvoice.ID) }
For integration testing, use the FreshBooks sandbox environment to avoid messing with your production data.
When deploying your integration:
And there you have it! You're now equipped to build a robust FreshBooks API integration in Go. Remember, the toggl/go-freshbooks
package documentation is your friend for more detailed information.
Happy coding, and may your invoices always be paid on time!