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.
API tokens must be kept private. In the event that one is exposed a new one should be generated.
Create a Client
- Go
- JavaScript (alpha)
- Python (alpha)
- Ruby (alpha)
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))
}
import 'isomorphic-fetch';
import {
Configuration,
} from '@statuscake/statuscake-js';
const config = new Configuration({
headers: {
'Authorization': `Bearer ${apiToken}`,
},
});
from statuscake import ApiClient
client = ApiClient(
header_name='Authorization',
header_value='Bearer %s' % api_token,
)
require 'statuscake'
client = StatusCake::ApiClient.new
# Constructing these options is a temporary fix until a client wide
# authentication mechanism has been realised.
opts = { header_params: { 'Authorization' => "Bearer #{api_token}" } }
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.
- Go
- JavaScript (alpha)
- Python (alpha)
- Ruby (alpha)
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))
}
const apiToken = process.env.STATUSCAKE_API_TOKEN;
if (!apiToken) {
throw new Error('STATUSCAKE_API_TOKEN is not set');
}
import os
api_token = os.environ['STATUSCAKE_API_TOKEN']
api_token = ENV.fetch('STATUSCAKE_API_TOKEN')
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.
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.