Authentication
The Craft API uses OAuth 2.0. Craft Identity issues the access token. Send the token as a bearer token on every GraphQL request.
Steps
- Request client credentials from Craft. You receive a client ID and a client secret.
- Exchange the credentials for an access token at the Craft Identity token endpoint.
- Send the token in the
Authorizationheader of each GraphQL request. - Refresh the token before it expires. The token response contains the lifetime in
expires_in.
Sample request
curl -X POST https://auth-api.craft.co/oidc/token \
-u "$CLIENT_ID:$CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "resource=https://api.craft.co"
Sample response
{
"access_token": "<jwt>",
"token_type": "Bearer",
"expires_in": 3600
}
Next call: use access_token as the bearer token on every GraphQL request.
curl -X POST https://api.craft.co/beta/graphql \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"query":"{ company(id: \"<company-id>\") { id name } }"}'
Error example
A missing or expired token returns HTTP 401. A token without access to the requested data returns an error in the top-level errors array. See Errors.
{
"errors": [{ "message": "Unauthorized" }]
}