godatadog

Trouble authenticating datadog api key using datadog-api-client-go package


I created an api key in datadog by going to Organization Setting > Api Key > New Key

On the datadog authentication docs this is how you test if your key is valid:

// Validate API key returns "OK" response

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "os"

    "github.com/DataDog/datadog-api-client-go/v2/api/datadog"
    "github.com/DataDog/datadog-api-client-go/v2/api/datadogV1"
)

func main() {
    ctx := datadog.NewDefaultContext(context.Background())
    configuration := datadog.NewConfiguration()
    apiClient := datadog.NewAPIClient(configuration)
    api := datadogV1.NewAuthenticationApi(apiClient)
    resp, r, err := api.Validate(ctx)

    if err != nil {
        fmt.Fprintf(os.Stderr, "Error when calling `AuthenticationApi.Validate`: %v\n", err)
        fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
    }

    responseContent, _ := json.MarshalIndent(resp, "", "  ")
    fmt.Fprintf(os.Stdout, "Response from `AuthenticationApi.Validate`:\n%s\n", responseContent)
}

Then it says to run:

DD_SITE="datadoghq.com" DD_API_KEY="<DD_API_KEY>" go run "main.go"

*** EDIT: Had to change datadoghq.com to us5.datadoghq.com for it to work (your region may vary)

Why am I getting 403 forbidden ;-;

I tried making new keys but I dont know what else to do


Solution

  • It's likely you're using the wrong hostname or creating the API keys in the wrong site. Ensure that you're using the correct site setting for your API call. In the top right of the docs page you referenced, there is a dropdown for picking your site which will update the required URL, e.g. GET https://api.datadoghq.eu/api/v1/validate and DD_SITE="datadoghq.eu" DD_API_KEY="<DD_API_KEY>" go run "main.go"