Skip to content

Auth API

The Auth API provides a single endpoint for refreshing Keycloak OIDC access tokens. It is only active when AUTH_PROVIDER=keycloak.


Exchange a Keycloak refresh token for a new access token and refresh token pair.

Use this endpoint when an access token has expired and you need to obtain a new one without requiring the user to re-authenticate.

{
"refresh_token": "eyJhbGciOiJSUzI1NiIsInR5..."
}
FieldTypeRequiredDescription
refresh_tokenstringYesA valid Keycloak refresh token
{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5...",
"refresh_token": "eyJhbGciOiJSUzI1NiIsInR5...",
"token_type": "bearer"
}
FieldDescription
access_tokenNew access token. Use as Authorization: Bearer <access_token> on all subsequent requests.
refresh_tokenNew refresh token. Replace the old one — Keycloak may rotate it.
token_typeAlways "bearer"
StatusCondition
401Refresh token is expired or invalid
500Keycloak server unreachable
Terminal window
# Refresh an expired token
RESPONSE=$(curl -s -X POST http://localhost:8080/auth/refresh \
-H "Content-Type: application/json" \
-d '{"refresh_token": "eyJhbGciOiJSUzI1NiIsInR5..."}')
ACCESS_TOKEN=$(echo $RESPONSE | jq -r '.access_token')
REFRESH_TOKEN=$(echo $RESPONSE | jq -r '.refresh_token')
# Use the new access token
curl -X POST "http://localhost:8080/event/infer/my-agent" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"query": "Hello", "user_id": "user-1", "session_id": "session-abc"}'

This endpoint is only relevant when AUTH_PROVIDER=keycloak. For api_token and jwt auth providers, tokens do not expire in the same way and this endpoint is not needed.

Typical usage pattern:

  1. Obtain an initial access token and refresh token from Keycloak directly (via the Keycloak login flow or client credentials grant).
  2. Use the access token on all Alquimia API requests.
  3. When a request returns 401, call POST /auth/refresh with the refresh token.
  4. Replace both tokens with the values from the response.
  5. Retry the original request with the new access token.

Set these environment variables to enable Keycloak auth:

Terminal window
AUTH_PROVIDER=keycloak
KEYCLOAK_SERVER_URL=https://keycloak.example.com
KEYCLOAK_REALM=my-realm
KEYCLOAK_CLIENT_ID=alquimia-runtime
KEYCLOAK_CLIENT_SECRET=<client-secret>
KEYCLOAK_ADMIN_CLIENT_SECRET=<admin-client-secret>
KEYCLOAK_CALLBACK_URI=https://runtime.example.com/auth/callback

See Configuration Reference for the full list of Keycloak settings.