Hey there, fellow developer! Ready to dive into the world of Kintone API integration using Go? You're in for a treat. Kintone's API is a powerhouse for customizing and extending your Kintone apps, and Go's simplicity and performance make it a perfect match. Let's get cracking!
Before we jump in, make sure you've got:
I'm assuming you're already familiar with Go basics and Kintone concepts. If not, no worries – just brush up on those real quick, and you'll be good to go.
First things first, let's set up our Go project:
mkdir kintone-go-integration cd kintone-go-integration go mod init kintone-go-integration
Now, let's grab the kintone package:
go get github.com/kintone/go-kintone
Time to get our hands dirty with some code:
package main import ( "fmt" "github.com/kintone/go-kintone" ) func main() { client := &kintone.Client{ Domain: "your-subdomain.kintone.com", User: "your-username", Password: "your-password", // Or use API token instead: // APIToken: "your-api-token", } // We'll use this client for all our operations }
Let's fetch some records:
app := &kintone.App{ Client: client, AppID: 123, // Replace with your app ID } records, err := app.GetRecords(nil) if err != nil { fmt.Printf("Error retrieving records: %v\n", err) return } for _, record := range records { fmt.Printf("Record ID: %s\n", record.ID()) }
Adding a new record is a breeze:
newRecord := kintone.NewRecord() newRecord.Set("Title", "My Awesome Title") newRecord.Set("Description", "This is a great description") id, err := app.AddRecord(newRecord) if err != nil { fmt.Printf("Error adding record: %v\n", err) return } fmt.Printf("Added new record with ID: %d\n", id)
Updating is just as easy:
updateRecord := kintone.NewRecord() updateRecord.Set("Title", "Updated Title") err = app.UpdateRecordByID(123, updateRecord) // Replace 123 with the actual record ID if err != nil { fmt.Printf("Error updating record: %v\n", err) return } fmt.Println("Record updated successfully")
Out with the old:
err = app.DeleteRecords([]uint64{123, 456}) // Replace with actual record IDs if err != nil { fmt.Printf("Error deleting records: %v\n", err) return } fmt.Println("Records deleted successfully")
Kintone has various field types. Here's how to handle a few:
// Date field newRecord.Set("Date", kintone.NewDate(2023, 5, 17)) // Dropdown field newRecord.Set("Category", kintone.SingleSelectField("Option 1")) // Attachment file, _ := os.Open("path/to/file.pdf") fileKey, _ := app.Upload("file.pdf", file) newRecord.Set("Attachment", kintone.FileField{&kintone.File{FileKey: fileKey}})
Always check for errors and respect rate limits:
if err != nil { // Log the error, maybe retry the operation return } // Consider adding a small delay between requests time.Sleep(200 * time.Millisecond)
Want to level up? Try bulk operations:
records := []*kintone.Record{ kintone.NewRecord().Set("Title", "Bulk 1"), kintone.NewRecord().Set("Title", "Bulk 2"), } ids, err := app.AddRecords(records) if err != nil { fmt.Printf("Error in bulk add: %v\n", err) return } fmt.Printf("Added %d records\n", len(ids))
Don't forget to test! Here's a simple example:
func TestAddRecord(t *testing.T) { // Set up a test client and app // ... newRecord := kintone.NewRecord().Set("Title", "Test Record") id, err := app.AddRecord(newRecord) assert.NoError(t, err) assert.NotZero(t, id) }
And there you have it! You're now equipped to build robust Kintone integrations with Go. Remember, this is just the tip of the iceberg. Explore the go-kintone
package documentation for more advanced features and don't be afraid to experiment.
Happy coding, and may your integrations be ever smooth and your errors few!