If you have decided to use one of the Bunny SDKs then you dont need to worry about this part as the SDK takes care of access token generation and including it as a header on each request for you.
If you've decided to build your own implementation and not use a Bunny SDK then you will need to do the following.
Generate an Access Token
Bunny uses the OAuth 2.0 protocol for issuing tokens and authorizing requests.
To generate an access token you will need the Client ID and Client Secret of Bunny API client application. .
To generate an access token we will be using the Client Credentials grant flow. This flow is designed to be a server side request so is not suitable for requesting tokens from a client side / javascript application.
Request
From your server side you will make the following POST request including your client_id, client_secret and the scope of access that you require for form encoded values.
You've likely requested a scope that is not approved for this API client application
{
"error": "invalid_scope",
"error_description": "The requested scope is invalid, unknown, or malformed."
}
The grant_type must be client_credentials
{
"error": "unsupported_grant_type",
"error_description": "The authorization grant type is not supported by the authorization server."
}
Either the client_id or client_secret has been entered incorrectly or the API client has been deleted.
{
"error": "invalid_client",
"error_description": "Client authentication failed due to unknown client, no client authentication included, or unsupported authentication method."
}
Set an authorization header
Add an Authorization header to your Bunny API request, including the access token as a bearer token.
Depending on the access token expiry length that was defined for your API client application at some point your token will expire and an API request will fail.
In this case you will want to generate a new access token and try the request again.
For example, if your API client application has an access token expiry of 7200 seconds then every 2 hours you would need to request a new token. If you proceeded to make a request after 2 hours with an expired token you would get the following response.
[
{
"description": [
"Token is expired"
]
}
]
In this case your code could watch out for the HTTP 401 status and attempt to generate a new access token. Then with the new access token you should retry the original API request.