Back

Step by Step Guide to Building a ServiceNow API Integration in Go

Aug 3, 20247 minute read

Introduction

Hey there, fellow Go enthusiast! Ready to dive into the world of ServiceNow API integration? You're in for a treat. We'll be using the servicenowsdkgo package to make our lives easier. Buckle up, and let's get coding!

Prerequisites

Before we jump in, make sure you've got:

  • Go installed on your machine (you're a Go dev, so I'm sure you've got this covered!)
  • A ServiceNow instance and API credentials (if you don't have these, give your friendly neighborhood ServiceNow admin a shout)
  • A basic understanding of Go and REST APIs (but hey, you're here, so I'm guessing you're good to go)

Setting Up the Project

Let's kick things off by creating a new Go module and grabbing the servicenowsdkgo package:

mkdir servicenow-integration && cd servicenow-integration go mod init servicenow-integration go get github.com/ServiceNow/servicenow-sdk-go

Initializing the ServiceNow Client

Now, let's get that ServiceNow client up and running:

package main import ( "fmt" "github.com/ServiceNow/servicenow-sdk-go" ) func main() { client := servicenow.NewClient("https://your-instance.service-now.com", "your-username", "your-password") // We're ready to rock and roll! }

Making API Requests

GET Request

Let's fetch some incidents:

incidents, err := client.Incident.GetAll() if err != nil { fmt.Printf("Error fetching incidents: %v\n", err) return } fmt.Printf("Found %d incidents\n", len(incidents))

POST Request

Creating a new record is a breeze:

newIncident := &servicenow.Incident{ ShortDescription: "Coffee machine is broken", Priority: "3", } createdIncident, err := client.Incident.Create(newIncident) if err != nil { fmt.Printf("Error creating incident: %v\n", err) return } fmt.Printf("Created incident with ID: %s\n", createdIncident.SysID)

PUT Request

Updating a record? No sweat:

updateData := map[string]interface{}{ "priority": "2", "state": "2", // In Progress } updatedIncident, err := client.Incident.Update("incident-sys-id", updateData) if err != nil { fmt.Printf("Error updating incident: %v\n", err) return } fmt.Printf("Updated incident: %+v\n", updatedIncident)

DELETE Request

Sometimes, you just gotta let go:

err := client.Incident.Delete("incident-sys-id") if err != nil { fmt.Printf("Error deleting incident: %v\n", err) return } fmt.Println("Incident deleted successfully")

Handling Responses

The servicenowsdkgo package does a lot of the heavy lifting for you, but always be prepared for errors:

incidents, err := client.Incident.GetAll() if err != nil { switch e := err.(type) { case *servicenow.APIError: fmt.Printf("API Error: %s\n", e.Error()) default: fmt.Printf("Unexpected error: %v\n", err) } return }

Advanced Usage

Querying with Filters

Want to get fancy with your queries? Try this:

query := servicenow.Query("priority=1^state=2") highPriorityIncidents, err := client.Incident.GetAllWithQuery(query)

Pagination

Don't let large datasets slow you down:

options := &servicenow.GetAllOptions{ Limit: 100, Offset: 0, } for { incidents, err := client.Incident.GetAllWithOptions(options) if err != nil { // Handle error } if len(incidents) == 0 { break } // Process incidents options.Offset += len(incidents) }

Best Practices

  • Respect rate limits: Use exponential backoff for retries
  • Log everything: It'll save you headaches later
  • Use environment variables for credentials: Keep 'em safe!

Testing the Integration

Here's a quick unit test to get you started:

func TestGetIncident(t *testing.T) { mockClient := &MockServiceNowClient{} mockClient.On("GetIncident", "incident-sys-id").Return(&servicenow.Incident{ SysID: "incident-sys-id", ShortDescription: "Test Incident", }, nil) incident, err := mockClient.GetIncident("incident-sys-id") assert.NoError(t, err) assert.Equal(t, "Test Incident", incident.ShortDescription) }

Conclusion

And there you have it! You're now armed and dangerous with ServiceNow API integration skills in Go. Remember, the servicenowsdkgo package is your friend - it's got your back with a ton of features we didn't even touch on here.

Keep exploring, keep coding, and most importantly, have fun! If you hit any snags, the ServiceNow community is always here to help. Now go forth and integrate!