Go Echo
Echo is an efficient web framework for Go, boasting both high performance and adaptability. Its vast array of middleware and plugins facilitate seamless integration into your application, simplifying the enhancement of features and functions. To further streamline your development, here’s a guide on integrating the Go Echo SDK with APIToolkit.
Create an Account or Sign In to APIToolkit to Generate Key
- Sign up / Sign in to the API dashboard
- Create a project
- Generate an API key for your project, and include a brief description of your work. And to prevent losing your key after it has been generated, remember to make a copy of it.
Integrate with Go’s Echo Framework
Assuming you have Go and Echo already set up:
a. Install necessary packages (if not):
go get -u github.com/labstack/echo/v4
b. Create a new Go file, e.g., main.go
c. Begin setting up your Echo server:
package main
import (
"github.com/labstack/echo/v4"
)
func main() {
e := echo.New()
// Define your routes here...
e.Start(":8080")
}
d. Integrate with APIToolkit:
Let’s write the code to initialize apitoolkit
with echo
:
package main
import (
"context"
"github.com/labstack/echo/v4"
apitoolkit "github.com/apitoolkit/apitoolkit-go"
)
func main() {
ctx := context.Background()
// Initialize the client using your apitoolkit.io generated apikey
apitoolkitClient, err := apitoolkit.NewClient(ctx, apitoolkit.Config{APIKey: "<APIKEY>"})
if err != nil {
panic(err)
}
e := echo.New()
// Register with the corresponding middleware of your choice.
// Assuming apitoolkit provides an EchoMiddleware function for the echo framework.
e.Use(apitoolkitClient.EchoMiddleware)
e.POST("/:slug/test", func(c echo.Context) error {
return c.String(http.StatusOK, "ok")
})
e.Start(":8080")
}
Replace "YOUR_GENERATED_API_KEY"
with the API key you generated.
Test the Integration
Run your Echo server and access the endpoint to see if the integration works.
Redacting Sensitive Fields with Go’s Echo and APIToolkit
While it’s possible to mark fields as redacted directly from the APIToolkit dashboard, this client also offers client-side redacting. By doing this on the client-side, sensitive fields won’t leave your server at all, ensuring that your data remains confined to your infrastructure.
For instance, if you have a request body like:
{
"user": {
"id": 123456789,
"name": "John Doe",
"password": "secretpassword123",
"creditCard": {
"number": "1234567890123456",
"expiry": "12/25"
}
}
}
And you wish to redact the password
and credit card number
fields, you’d configure the apitoolkit
config struct as follows:
package main
import (
"github.com/labstack/echo/v4"
"net/http"
"apitoolkit-go"
)
func main() {
e := echo.New()
apitoolkitCfg := apitoolkit.Config{
RedactHeaders: []string{"Content-Type", "Authorization", "Cookies"}, // Redacting both request and response headers
RedactRequestBody: []string{"$.user.password", "$.user.creditCard.number"},
RedactResponseBody: []string{"$.message.error"},
APIKey: "<APIKEY>",
}
// Initialize the client with your apitoolkit.io generated API key
apitoolkitClient, _ := apitoolkit.NewClient(apitoolkitCfg)
// Use the APIToolkit middleware with Echo
e.Use(apitoolkitClient.EchoMiddleware) // Assuming APIToolkit provides such a middleware. Replace with the actual function if different.
e.POST("/:slug/test", func(c echo.Context) error {
return c.String(http.StatusOK, "ok")
})
e.Start(":8080")
}
It’s important to emphasize that while the RedactHeaders
config field accepts header names (case insensitive), RedactRequestBody
and RedactResponseBody
expect JSONPath strings.
JSONPath provides a flexible way to define sensitive fields in your responses. This list will be applied across all endpoint requests and responses. For more on JSONPath and how to craft queries, check out: JSONPath Cheatsheet
Outgoing Requests
To monitor outgoing HTTP requests from your Go application, you can 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 an apitoolkit server for monitoring and analysis.
Example
package main
import (
"github.com/labstack/echo/v4"
"net/http"
apitoolkit "github.com/apitoolkit/apitoolkit-go"
)
func main() {
router := echo.New()
router.Use(client.EchoMiddleware)
router.POST("/:slug/test", func(c echo.Context) (err error) {
// Create a new HTTP client
HTTPClient := http.DefaultClient
// Replace the transport with the custom roundtripper
HTTPClient.Transport = client.WrapRoundTripper(
c.Request().Context(),
HTTPClient.Transport,
WithRedactHeaders([]string{}),
)
// Make an outgoing HTTP request using the modified HTTPClient
_, _ = HTTPClient.Get("http://localhost:3000/monitored-outgoing-request")
// Respond to the request
c.String(http.StatusOK, "ok")
})
}
The provided code demonstrates how to set up the custom roundtripper to replace the default HTTP client’s transport. The resulting HTTP client, HTTPClient
, is configured to send copies of all incoming and outgoing requests to the apitoolkit servers. You can use this modified HTTP client for any HTTP requests you need to make from your server, ensuring they are monitored by apitoolkit.
Report Errors
If you’ve used sentry, or bugsnag, or rollbar, then you’re already familiar with this usecase.
But you can report an error to apitoolkit. A difference, is that errors are always associated with a parent request, and helps you query and associate the errors which occured while serving a given customer request. To request errors to APIToolkit use call the ReportError
method of apitoolkit
not the client returned by apitoolkit.NewClient
with the request context and the error to report
Example:
package main
import (
//... other imports
apitoolkit "github.com/apitoolkit/apitoolkit-go"
)
func main() {
e := echo.New()
ctx := context.Background()
apitoolkitClient, err := apitoolkit.NewClient(ctx, apitoolkit.Config{APIKey: "<API_KEY>"})
if err != nil {
panic(err)
}
e.Use(apitoolkitClient.EchoMiddleware)
e.GET("/", hello)
e.Logger.Fatal(e.Start(":1323"))
}
func hello(c echo.Context) error {
file, err := os.Open("non-existing-file.txt")
if err != nil {
apitoolkit.ReportError(c.Request().Context(), err)
}
log.Println(file)
return c.String(http.StatusOK, "Hello, World!")
}
Next Steps
- Deploy your application or send test HTTP requests to your service.
- Head over to the API log explorer or Endpoints section on the APIToolkit dashboard to confirm that your test request has been processed correctly.
- Enjoy having our API comanage your backends and APIs with you.