APItoolkit full color logo
Sign Up

Go Fiber SDK Guide

To integrate your Golang Fiber 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/fiber

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

package main

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

Configuration

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

package main

import (
  "context"

  "github.com/gofiber/fiber/v2"
  apitoolkit "github.com/apitoolkit/apitoolkit-go/fiber"
)

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

  // Initialize the APItoolkit client
  apitoolkitClient, err := apitoolkit.NewClient(
    ctx,
    apitoolkit.Config{
      APIKey: "{ENTER_YOUR_API_KEY_HERE}",
      Debug = false,
      Tags = []string{"environment: production", "region: us-east-1"},
      ServiceVersion: "v2.0",
    },
  )
  if err != nil {
    panic(err)
  }

  router := fiber.New()

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

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

  router.Get("/greet", func(c *fiber.Ctx) error {
    name := c.Query("name", "jon")
    return c.SendString("Hello " + name)
  })

  router.Listen(":3000")
}

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"
  "os"

  "github.com/gofiber/fiber/v2"
  apitoolkit "github.com/apitoolkit/apitoolkit-go/fiber"
)

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, err := apitoolkit.NewClient(ctx, apitoolkitCfg)
  if err != nil {
    panic(err)
  }

  router := fiber.New()
  router.Use(apitoolkit.FiberMiddleware(apitoolkitClient))

  router.Get("/", hello)

  router.Listen(":3000")
}

func hello(c *fiber.Ctx) error {
  // Attempt to open a non-existing file
  file, err := os.Open("non-existing-file.txt")
  if err != nil {
    // Report the error to APItoolkit
    apitoolkit.ReportError(c.Context(), err)
    return c.SendString("Something went wrong")
  }
  return c.SendString("Hello file, " + file.Name())
}

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 manually report specific errors at different parts of your application, use the ReportError() method, passing in the context and error arguments, like so:

package main

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

  "github.com/gofiber/fiber/v2"
  apitoolkit "github.com/apitoolkit/apitoolkit-go/fiber"
)

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

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

  router := fiber.New()
  router.Use(apitoolkit.FiberMiddleware(apitoolkitClient))

  router.GET("/", hello)

  router.Listen(":3000")
}

func hello(c *fiber.Ctx) error {
  // Attempt to open a non-existing file
  file, err := os.Open("non-existing-file.txt")
  if err != nil {
    // Report the error to APItoolkit
    apitoolkit.ReportError(c.Context(), err)
    return c.SendString("Something went wrong")
  }
  return c.SendString("Hello file, " + file.Name())
}

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 the configuration with a custom RoundTripper:

package main

import (
  "context"
  "net/http"

  "github.com/gofiber/fiber/v2"
  apitoolkit "github.com/apitoolkit/apitoolkit-go/fiber"
)

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

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

  router := fiber.New()
  router.Use(apitoolkit.FiberMiddleware(apitoolkitClient))

  router.Get("/test", func(c *fiber.Ctx) error {

    // Create a new HTTP client
    HTTPClient := apitoolkit.HTTPClient(
      c.Request().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
    return c.SendString("Ok, success!")
  })

  router.Listen(":3000")
}

Tip

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

If you are using a TLS client for your HTTP requests, you will need to use the apitoolkit-go/tls_client package to monitor those requests. To use the package, you must first install it using the command below:

go get github.com/apitoolkit/apitoolkit-go/tls_client

Here's an example of the configuration with a TLS client:

package main

import (
  "context"
  "net/http"

  "github.com/gofiber/fiber/v2"
  fhttp "github.com/bogdanfinn/fhttp"
  tls_client "github.com/bogdanfinn/tls-client"

  apitoolkit "github.com/apitoolkit/apitoolkit-go/fiber"
  apitoolkitTlsClient "github.com/apitoolkit/apitoolkit-go/tls_client"
)

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

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

  router := fiber.New()
  router.Use(apitoolkit.FiberMiddleware(apitoolkitClient))

  jar := tls_client.NewCookieJar()
  options := []tls_client.HttpClientOption{
    tls_client.WithTimeoutSeconds(30),
    tls_client.WithNotFollowRedirects(),
    tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
  }

  clientTLS, err := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
  if err != nil {
    panic(err)
  }

  router.Get("/test", func(c *fiber.Ctx) error {
    // Create a new apitoolkit custom TLS Client
    tclient := apitoolkitTlsClient.NewHttpClient(c.Request().Context(), clientTLS, apitoolkitClient)
    req, err := fhttp.NewRequest(http.MethodGet, "https://jsonplaceholder.typicode.com/posts/1", nil)
    if err != nil {
      panic(err)
    }

    // Make an outgoing HTTP request using the modified TLS Client
    resp, err := tclient.Do(req)
    if err != nil {
      panic(err)
    }
    log.Printf("status code: %d", resp.StatusCode)

    // Respond to the request
    return c.SendString("Ok, success!")
  })

  router.Listen(":3000")
}


Explore the Golang SDK