This repository contains a Go API client for the Datadog API.
- Go 1.17+
This repository contains per-major-version API client packages. Right
now, Datadog has two API versions, v1
, v2
and the common package, common
.
The client library for Datadog API v1 is located in the api/datadogV1
directory. Import it with
import "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
The client library for Datadog API v2 is located in the api/datadogV2
directory. Import it with
import "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
The datadog package for Datadog API is located in the api/datadog
directory. Import it with
import "github.com/DataDog/datadog-api-client-go/v2/api/datadog"
Here's an example creating a user:
package main
import (
"context"
"fmt"
"os"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)
func main() {
ctx := context.WithValue(
context.Background(),
datadog.ContextAPIKeys,
map[string]datadog.APIKey{
"apiKeyAuth": {
Key: os.Getenv("DD_CLIENT_API_KEY"),
},
"appKeyAuth": {
Key: os.Getenv("DD_CLIENT_APP_KEY"),
},
},
)
body := *datadogV2.NewUserCreateRequest(*datadogV2.NewUserCreateData(*datadogV2.NewUserCreateAttributes("jane.doe@example.com"), datadogV2.UsersType("users")))
configuration := datadog.NewConfiguration()
apiClient := datadog.NewAPIClient(configuration)
usersApi := datadogV2.NewUsersApi(apiClient)
resp, r, err := usersApi.CreateUser(ctx, body)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating user: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
responseData := resp.GetData()
fmt.Fprintf(os.Stdout, "User ID: %s", responseData.GetId())
}
Save it to example.go
, then run go get github.com/DataDog/datadog-api-client-go/v2
.
Set the DD_CLIENT_API_KEY
and DD_CLIENT_APP_KEY
to your Datadog
credentials, and then run go run example.go
.
This client includes access to Datadog API endpoints while they are in an unstable state and may undergo breaking changes. An extra configuration step is required to enable these endpoints:
configuration.SetUnstableOperationEnabled("<APIVersion>.<OperationName>", true)
where <OperationName>
is the name of the method used to interact with that endpoint. For example: GetLogsIndex
, or UpdateLogsIndex
When talking to a different server, like the eu
instance, change the ContextServerVariables
:
ctx = context.WithValue(ctx,
common.ContextServerVariables,
map[string]string{
"site": "datadoghq.eu",
})
If you want to disable GZIP compressed responses, set the compress
flag
on your configuration object:
configuration.Compress = false
If you want to enable requests logging, set the debug
flag on your configuration object:
configuration.Debug = true
Several listing operations have a pagination method to help consume all the items available. For example, to retrieve all your incidents:
package main
import (
"context"
"fmt"
"os"
"github.com/DataDog/datadog-api-client-go/v2/api/datadog"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
)
func main() {
ctx := datadog.NewDefaultContext(context.Background())
configuration := datadog.NewConfiguration()
configuration.SetUnstableOperationEnabled("v2.ListIncidents", true)
apiClient := datadog.NewAPIClient(configuration)
incidentsApi := datadogV2.NewIncidentsApi(apiClient)
resp, _, err := incidentsApi.ListIncidentsWithPagination(ctx, *datadog.NewListIncidentsOptionalParameters())
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `IncidentsApi.ListIncidents`: %v\n", err)
}
for incident := range resp {
fmt.Fprintf(os.Stdout, "Got incident %s\n", incident.GetId())
}
}
Documentation for API endpoints and models is available on pkg.go.dev.
As most of the code in this repository is generated, we will only accept PRs for files that are not modified by our code-generation machinery (changes to the generated files would get overwritten). We happily accept contributions to files that are not autogenerated, such as tests and development tooling.