Hey there, fellow Go enthusiast! Ready to dive into the world of time tracking with Clockify? In this guide, we'll walk through building a slick Clockify API integration using Go. We'll be leveraging the awesome go-clockify
package to make our lives easier. Let's get cracking!
Before we jump in, make sure you've got:
Let's kick things off by setting up our project:
mkdir clockify-integration cd clockify-integration go mod init github.com/yourusername/clockify-integration go get github.com/lucassabreu/clockify-api-client-go
Now, let's get that Clockify client up and running:
package main import ( "fmt" "github.com/lucassabreu/clockify-api-client-go" ) func main() { client, err := clockify.NewClient("your-api-key") if err != nil { panic(err) } // We're ready to rock! }
Let's start with some basic operations to get our feet wet:
// Fetch user info user, err := client.GetCurrentUser() if err != nil { panic(err) } fmt.Printf("Hello, %s!\n", user.Name) // Get workspaces workspaces, err := client.GetWorkspaces() if err != nil { panic(err) } fmt.Printf("You have %d workspaces\n", len(workspaces)) // Fetch time entries entries, err := client.GetTimeEntries(workspaces[0].ID, nil) if err != nil { panic(err) } fmt.Printf("Found %d time entries\n", len(entries))
Ready to level up? Let's create, update, and delete time entries:
// Create a time entry newEntry, err := client.CreateTimeEntry(workspaces[0].ID, clockify.TimeEntryRequest{ Start: time.Now(), End: time.Now().Add(time.Hour), ProjectID: "project-id", Description: "Coding like a boss", }) // Update a time entry updatedEntry, err := client.UpdateTimeEntry(workspaces[0].ID, newEntry.ID, clockify.TimeEntryRequest{ Description: "Refactoring like a ninja", }) // Delete a time entry err = client.DeleteTimeEntry(workspaces[0].ID, newEntry.ID)
Always handle your errors gracefully and keep an eye on rate limits:
if err != nil { if clockifyErr, ok := err.(*clockify.Error); ok { fmt.Printf("Clockify API error: %s\n", clockifyErr.Message) } else { fmt.Printf("Unexpected error: %v\n", err) } }
Let's wrap this up in a neat little CLI package:
package main import ( "flag" "fmt" "github.com/lucassabreu/clockify-api-client-go" ) func main() { apiKey := flag.String("api-key", "", "Clockify API key") flag.Parse() if *apiKey == "" { fmt.Println("Please provide an API key") return } client, err := clockify.NewClient(*apiKey) if err != nil { panic(err) } // Add your operations here }
Don't forget to test your code! Here's a quick example:
func TestGetCurrentUser(t *testing.T) { client, _ := clockify.NewClient("mock-api-key") user, err := client.GetCurrentUser() assert.NoError(t, err) assert.Equal(t, "John Doe", user.Name) }
And there you have it! You've just built a solid Clockify API integration in Go. Remember, this is just scratching the surface - there's plenty more you can do with the Clockify API. Keep exploring, keep coding, and most importantly, keep tracking that time!
For more details, check out the go-clockify documentation and the Clockify API docs.
Now go forth and conquer those time tracking challenges! 🚀