Go
DataDome Go Integration build detects and protects against bot activity
This documentation is for version
2.x.x
- If you are running with version
1.x.x
of the Go module, please follow this documentation.- If you are upgrading your integration from version
1.x.x
to version2.x.x
, please follow this guide to ensure a successful migration.
Compatibility
- Go >= 1.20
Prerequisites
- The server-side key available in your DataDome dashboard
Installation
The module can be installed with the go get
command:
go get github.com/datadome/module-go-package/v2
Usage
The following example uses the Gin framework and the DataDome module as a middleware:
package main
import (
"log"
"net/http"
dd "github.com/datadome/module-go-package/v2"
"github.com/gin-gonic/gin"
)
// DataDomeMiddleware verifies requests and return the response from the Protection API
func DataDomeMiddleware(d *dd.Client) gin.HandlerFunc {
return func(c *gin.Context) {
isBlocked, err := d.DatadomeProtect(c.Writer, c.Request)
if err != nil {
log.Println("error occured during protection: %v", err)
}
if isBlocked {
c.AbortWithStatus(403)
return
}
c.Next()
}
}
func main() {
d, _ := dd.NewClient("DATADOME_SERVER_SIDE_KEY")
router := gin.Default()
router.Use(DataDomeMiddleware(d))
router.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "Hello world"})
return
})
router.Run(":8080")
}
Congrats! You can now see your traffic in your DataDome dashboard.
Configuration
The module can be configured by customizing the Client
structure with optional functions.
Refer to the Settings section below for the full list of possible configurations.
Settings
Settings | Functional option | Description | Required | Default value |
---|---|---|---|---|
EnableGraphQLSupport | WithGraphQLSupport | Enables the support of GraphQL requests. | No | false |
EnableReferrerRestoration | WithReferrerRestoration | Restores original referrer after a challenge is passed. | No | false |
Endpoint | WithEndpoint | Host of the Protection API. | No | api.datadome.co |
Logger | WithLogger | The level-based Logger of your choice. | No | The standard log package |
Timeout | WithTimeout | Timeout in milliseconds, after which the request will be allowed. | No | 150 |
UrlPatternExclusion | WithUrlPatternExclusion | Regex to match to exclude requests from being processed with the Protection API. If not defined, all requests will be processed. | No | (?i)\.(avi|flv|mka|mkv|mov|mp4|mpeg|mpg|mp3|flac|ogg|ogm|opus|wav|webm|webp|bmp|gif|ico|jpeg|jpg|png|svg|svgz|swf|eot|otf|ttf|woff|woff2|css|less|js|map|json)$ |
UrlPatternInclusion | WithUrlPatternInclusion | Regex to match to process the request with the Protection API. If not defined, all requests that don't match UrlPatternExclusion will be processed. | No | - |
You can find a usage example of the functional options in the snippet below:
client, err := dd.NewClient(
"Some key",
dd.WithEndpoint("api.datadome.co"),
dd.WithTimeout(150),
dd.WithGraphQLSupport(true),
dd.WithReferrerRestoration(true),
dd.WithUrlPatternExclusion(`(?i)\.(avi|flv|mka|mkv|mov|mp4|mpeg|mpg|mp3|flac|ogg|ogm|opus|wav|webm|webp|bmp|gif|ico|jpeg|jpg|png|svg|svgz|swf|eot|otf|ttf|woff|woff2|css|less|js|map|json)$`),
dd.WithUrlPatternInclusion(`(?i)\/included\/endpoint`),
dd.Logger(dd.NewDefaultLogger()),
)
FAQ
How do I restore the Referer
request header after a challenge is passed?
Referer
request header after a challenge is passed?After passing a DataDome challenge on browsers other than Firefox, the referrer value is updated to the current URL which can lead to inconsistent results in website analytics.
Since version 1.3.0
, it is possible to restore the Referer
header to its original value for your backend:
- Contact our support team, they will review your requirements and provide you with the best recommendations.
- Set the boolean value of the
EnableReferrerRestoration
setting totrue
.
Updated 25 days ago