APItoolkit full color logo
Sign Up

Go Chi SDK Guide

To integrate your Golang Chi application with APItoolkit, you need to use this SDK to monitor incoming traffic, aggregate the requests, and then send them to APItoolkit's servers. Kindly follow this guide to get started and learn about all the supported features of APItoolkit's Golang SDK.


Prerequisites

Ensure you have already completed the first three steps of the onboarding guide.

Installation

Kindly run the command below to install the SDK:

go get github.com/apitoolkit/apitoolkit-go

Then add github.com/apitoolkit/apitoolkit-go to the list of imports, like so:

package main

import (
  apitoolkit "github.com/apitoolkit/apitoolkit-go"
)

Configuration

Next, initialize APItoolkit in your application's entry point (e.g., main.go), like so:

Here is your Go code formatted with two spaces instead of tabs:

package main

import (
  "context"
  "net/http"

  "github.com/go-chi/chi/v5"
  apitoolkit "github.com/apitoolkit/apitoolkit-go"
)

func main() {
  ctx := context.Background()

  // Initialize the APItoolkit client
  apitoolkitClient, err := apitoolkit.NewClient(
    ctx,
    apitoolkit.Config{APIKey: "{ENTER_YOUR_API_KEY_HERE}"},
  )
  if err != nil {
    panic(err)
  }

  router := chi.NewRouter()

  // Register APItoolkit's middleware
  router.Use(apitoolkitClient.ChiMiddleware)

  // router.Use(...)
  // Other middleware

  router.Get("/test", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    w.Write([]byte("ok"))
  })

  http.ListenAndServe(":3000", router)
}

Tip

The {ENTER_YOUR_API_KEY_HERE} demo string should be replaced with the API key generated from the APItoolkit dashboard.

Redacting Sensitive Data

If you have fields that are sensitive and should not be sent to APItoolkit servers, you can mark those fields to be redacted (the fields will never leave your servers).

To mark a field for redacting via this SDK, you need to provide additional arguments to the apitoolkitCfg variable with paths to the fields that should be redacted. There are three arguments you can provide to configure what gets redacted, namely:

  1. RedactHeaders: A list of HTTP header keys.
  2. RedactRequestBody: A list of JSONPaths from the request body.
  3. RedactResponseBody: A list of JSONPaths from the response body.


JSONPath is a query language used to select and extract data from JSON files. For example, given the following sample user data JSON object:

{
  "user": {
    "name": "John Martha",
    "email": "[email protected]",
    "addresses": [
      {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      },
      {
        "street": "123 Main St",
        "city": "Anytown",
        "state": "CA",
        "zip": "12345"
      }
    ],
    "credit_card": {
      "number": "4111111111111111",
      "expiration": "12/28",
      "cvv": "123"
    }
  }
}

Examples of valid JSONPath expressions would be:

JSONPathDescription
$.user.addresses[*].zipIn this case, APItoolkit will replace the zip field in all the objects of the addresses list inside the user object with the string [CLIENT_REDACTED].
$.user.credit_cardIn this case, APItoolkit will replace the entire credit_card object inside the user object with the string [CLIENT_REDACTED].

Tip

To learn more about JSONPaths, please take a look at the official docs or use this JSONPath Evaluator to validate your JSONPath expressions.

You can also use our JSON Redaction Tool to preview what the final data sent from your API to APItoolkit will look like, after redacting any given JSON object.


Here's an example of what the configuration would look like with redacted fields:

package main

import (
  "context"
  "net/http"

  "github.com/go-chi/chi/v5"
  apitoolkit "github.com/apitoolkit/apitoolkit-go"
)

func main() {
  ctx := context.Background()

  apitoolkitCfg := apitoolkit.Config{
    APIKey:             "{ENTER_YOUR_API_KEY_HERE}",
    RedactHeaders:      []string{"content-type", "Authorization", "HOST"},
    RedactRequestBody:  []string{"$.user.email", "$.user.addresses"},
    RedactResponseBody: []string{"$.users[*].email", "$.users[*].credit_card"},
  }
  apitoolkitClient, _ := apitoolkit.NewClient(ctx, apitoolkitCfg)

  router := chi.NewRouter()
  router.Use(apitoolkitClient.ChiMiddleware)

  router.Get("/test", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK)
    w.Write([]byte("ok"))
  })

  http.ListenAndServe(":3000", router)
}

Note

  • The RedactHeaders config field expects a list of case-insensitive headers as strings.
  • The RedactRequestBody and RedactResponseBody config fields expect a list of JSONPaths as strings.
  • The list of items to be redacted will be applied to all endpoint requests and responses on your server.

Error Reporting

APItoolkit automatically detects different unhandled errors, API issues, and anomalies but you can report and track specific errors at different parts of your application. This will help you associate more detail and context from your backend with any failing customer request.

To report errors, use the ReportError() method, passing in the context and error arguments, like so:

package main

import (
  "context"
  "fmt"
  "net/http"
  "os"

  "github.com/go-chi/chi/v5"
  apitoolkit "github.com/apitoolkit/apitoolkit-go"
)

func main() {
  ctx := context.Background()

  apitoolkitClient, err := apitoolkit.NewClient(
    ctx,
    apitoolkit.Config{APIKey: "{ENTER_YOUR_API_KEY_HERE}"},
  )
  if err != nil {
    panic(err)
  }

  router := chi.NewRouter()
  router.Use(apitoolkitClient.ChiMiddleware)

  router.Get("/greet", hello)

  http.ListenAndServe(":3000", router)
}

func hello(w http.ResponseWriter, r *http.Request) {
  // Attempt to open a non-existing file
  _, err := os.Open("non-existing-file.txt")
  if err != nil {
    // Report the error to APItoolkit
    apitoolkit.ReportError(r.Context(), err)
  }
  fmt.Fprintln(w, "Hello, World!")
}

Tip

The ReportError() method mentioned above is imported directly from apitoolkit and not apitoolkitClient.

Monitoring Outgoing Requests

Outgoing requests are external API calls you make from your API. By default, APItoolkit monitors all requests users make from your application and they will all appear in the API Log Explorer page. However, you can separate outgoing requests from others and explore them in the Outgoing Integrations page, alongside the incoming request that triggered them.

To monitor outgoing HTTP requests from your application, replace the default HTTP client transport with a custom RoundTripper. This allows you to capture and send copies of all incoming and outgoing requests to APItoolkit. Here's an example of outgoing requests configuration with this SDK:

package main

import (
  "context"
  "net/http"

  "github.com/go-chi/chi/v5"
  apitoolkit "github.com/apitoolkit/apitoolkit-go"
)

func main() {
  ctx := context.Background()

  apitoolkitClient, err := apitoolkit.NewClient(
    ctx,
    apitoolkit.Config{APIKey: "{ENTER_YOUR_API_KEY_HERE}"},
  )
  if err != nil {
    panic(err)
  }

  router := chi.NewRouter()
  router.Use(apitoolkitClient.ChiMiddleware)

  router.Get("/test", func(w http.ResponseWriter, r *http.Request) {

    // Create a new HTTP client
    HTTPClient := apitoolkit.HTTPClient(
      r.Context(),
      apitoolkit.WithRedactHeaders("content-type", "Authorization", "HOST"),
      apitoolkit.WithRedactRequestBody("$.user.email", "$.user.addresses"),
      apitoolkit.WithRedactResponseBody("$.users[*].email", "$.users[*].credit_card"),
    )

    // Make an outgoing HTTP request using the modified HTTPClient
    _, _ = HTTPClient.Get("https://jsonplaceholder.typicode.com/posts/1")

    // Respond to the request
    w.WriteHeader(http.StatusOK)
    w.Write([]byte("Ok, success!"))
  })

  http.ListenAndServe(":3000", router)
}

Tip

You can also redact data with the custom RoundTripper for outgoing requests.


Explore the Golang SDK