This page guides you through the steps of configuring your Gin application to work with Apitally. If you don’t have an account yet, now would be a good time to sign up.

Once you’re done with this guide, you will be able to:

  • Analyze requests to your application
  • Keep track of errors, response times and payload sizes
  • Understand how individual API consumers use your application
  • Log and inspect API requests and responses
  • Monitor your application’s uptime and receive alerts when it’s down
  • Set up custom alert rules and get notified when they’re triggered

Create app

To get started, create a new app in the Apitally dashboard and select as your framework.

Here you can also configure the environments (e.g. prod and dev) for your app, or simply accept the defaults.

Take a note of the client ID displayed after submitting. You will need it in the next step.

Add middleware

Add the Apitally SDK to the dependencies in your Gin project.

go get github.com/apitally/apitally-go/gin

Add the Apitally middleware to your Gin application and provide the ClientId for your app. You’ll find the ClientId on the Setup instructions page for your app in the Apitally dashboard, which is displayed immediately after creating the app.

package main

import (
    apitally "github.com/apitally/apitally-go/gin"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    config := &apitally.Config{
        ClientId: "your-client-id",
        Env:      "dev", // or "prod" etc.
    }
    r.Use(apitally.Middleware(r, config))

    // ... rest of your code ...
}

Add the Apitally middleware before any other middleware to ensure it wraps the entire stack.

Deploy your application with these changes, or restart if you're testing locally.

At this point the basic setup for your application is complete and you will start seeing data in the Apitally dashboard. However, you won't be able to analyze API consumers yet.

Identify consumers

To analyze and filter API traffic by consumers, you can associate requests with consumer identifiers in your application.

In most cases, use the authenticated identity to identify the consumer. The identifier should be a string, such as a username, email address, or any other unique identifier.

Optionally, you can also provide a display name and group for each consumer.

To associate requests with consumers, use the SetConsumerIdentifier or SetConsumer function, for example in a middleware.

func authMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        // Your authentication logic here
        // ...

        if user.IsAuthenticated() {
            // Use the authenticated identity as consumer identifier
            apitally.SetConsumerIdentifier(c, user.Identifier)
        }
        c.Next()
    }
}

Now the Consumers page in the Apitally dashboard shows you information about all consumers that have made requests to your application. You can also filter insights on other the dashboards by consumer and better understand how each of them use your application.

Capture validation errors

In order to get insights into validation errors returned by your API endpoints, you can capture them using the CaptureValidationError function.

package main

import (
    apitally "github.com/apitally/apitally-go/gin"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    // ...

    r.POST("/hello", func(c *gin.Context) {
        var req struct {
            Name string `json:"name" binding:"required"`
        }
        if err := c.BindJSON(&req); err != nil {
            apitally.CaptureValidationError(c, err)
            c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
            return
        }
        c.JSON(http.StatusOK, gin.H{
            "message": "Hello, " + req.Name + "!",
        })
    })

    // ...
}

Configure request logging

Logging of individual requests and responses is disabled by default to protect potentially sensitive data. If you enable it, you can configure in detail what parts of the request and response should be logged. You can also mask sensitive information (e.g. in headers) and exclude certain requests from logging.

The SDK applies default masking rules for common sensitive headers, query parameters and request/response body fields.

package main

import (
    apitally "github.com/apitally/apitally-go/gin"
    "github.com/gin-gonic/gin"
)

func main() {
    r := gin.Default()

    config := &apitally.Config{
        ClientId: "your-client-id",
        Env:      "dev", // or "prod" etc.
        RequestLoggingConfig: &apitally.RequestLoggingConfig{
            Enabled:            true,
            LogQueryParams:     true,
            LogRequestHeaders:  true,
            LogRequestBody:     true,
            LogResponseHeaders: true,
            LogResponseBody:    true,
            LogPanic:           true,
        },
    }
    r.Use(apitally.Middleware(r, config))

    // ... rest of your code ...
}