Skip to main content

Authentication

Once the desired SDK is installed the next step is to create an authenticated API client. You can view and manage your API tokens from the StatusCake account panel.

warning

API tokens must be kept private. In the event that one is exposed a new one should be generated.

Create a Client​

Create a Client
package main

import (
"github.com/StatusCakeDev/statuscake-go"
"github.com/StatusCakeDev/statuscake-go/credentials"
)

func main() {
bearer := credentials.NewBearerWithStaticToken("my-api-token")
client := statuscake.NewClient(statuscake.WithRequestCredentials(bearer))
}

Reading the Token from the Environment​

Do not write a token into your source code. Read it from the environment of the process, which is where deployment platforms and CI systems expect to place secrets. STATUSCAKE_API_TOKEN is the name used by the other StatusCake tools, so a single variable can serve them all.

Token from the environment
package main

import (
"os"

"github.com/StatusCakeDev/statuscake-go"
"github.com/StatusCakeDev/statuscake-go/credentials"
)

func main() {
apiToken := os.Getenv("STATUSCAKE_API_TOKEN")
if apiToken == "" {
panic("STATUSCAKE_API_TOKEN is not set")
}

bearer := credentials.NewBearerWithStaticToken(apiToken)
client := statuscake.NewClient(statuscake.WithRequestCredentials(bearer))
}

Tokens That Change​

A static token is fixed when the client is created. A long-running service may need to pick up a rotated token without restarting. The Go SDK supports this by accepting a function, which is called for every request, in place of a fixed value.

A token supplied by a function
package main

import (
"net/http"
"os"

"github.com/StatusCakeDev/statuscake-go"
"github.com/StatusCakeDev/statuscake-go/credentials"
)

func main() {
bearer := credentials.NewBearer(func(*http.Request) string {
return os.Getenv("STATUSCAKE_API_TOKEN")
})
client := statuscake.NewClient(statuscake.WithRequestCredentials(bearer))
_ = client
}

The function is given the request about to be sent, so it can fetch a token from a secrets manager or a cache. Keep it fast, since it runs before every request.

Next Steps​

Make your first requests in the Quick Start.