Skip to main content

Call the API with a scoped key

Create an API key that may do one job, use it from a script, and rotate it.

RecipeReadyView source
Packages
lumo
Works with
lumo CLIPlain HTTPPythonJavaScript / TypeScript

What it shows​

  1. You create an API key for one job: a nightly report that lists the users. The key gets one scope, admin:users:read.
  2. The script lists the users with the key. LumoAuth allows the call.
  3. The script asks for the roles. LumoAuth refuses, and the answer names the missing scope.
  4. You rotate the key in the portal. The old key stops working at once.

Before you start​

You create the key in the portal. Your account needs the permission settings.manage.

  1. In the sidebar, under Applications & developer tools, open API keys, and click Create API key.
  2. Enter the Name Nightly user report, and choose an Expiration, for example 30 days.
  3. Under Permissions, tick admin:users:read and nothing else.
  4. Click Create API key. The portal can ask you to confirm your sign-in first.
  5. Copy the key from the page API key created successfully. LumoAuth shows it only once.

The Python version uses the package requests. The CLI version needs the lumo CLI.

VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
REPORT_API_KEYThe API key of this recipe, with the one scope admin:users:read.

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization.

authorization/api-keys/cli/report.sh
#!/bin/sh
# Call the Admin API with a key that has one scope, and read the refusal for
# a call outside that scope, with the lumo CLI.
#
# sh authorization/api-keys/cli/report.sh
set -eu

# The CLI reads its own three variables. With a key it needs no `lumo login`.
export LUMO_BASE_URL="$LUMOAUTH_URL" LUMO_ORG="$LUMOAUTH_ORG_ID" LUMO_API_KEY="$REPORT_API_KEY"

get() {
# In JSON mode an error is printed as JSON, with the status and the details.
if answer=$(lumo api GET "$2" --query limit=1 --output json); then
echo "$1 -> ok"
return
fi
answer=$(printf '%s\n' "$answer" | tr -d ' \n')
case "$answer" in
# A refusal for a missing scope names the scope.
*'"status":403'*) echo "$1 -> refused, missing scope: $(printf '%s\n' "$answer" | sed -n 's/.*"required_scopes":\["\([^"]*\)".*/\1/p')" ;;
*'"status":401'*) echo "$1 -> refused, the key is not valid" ;;
*) echo "$answer"; exit 1 ;;
esac
}

# The key has the scope admin:users:read.
get "list users" /users
# Roles need admin:roles:read, which the key does not have.
get "list roles" /roles
sh authorization/api-keys/cli/report.sh

Check the result​

Output
list users -> ok
list roles -> refused, missing scope: admin:roles:read

Then rotate the key:

  1. On the API keys page, click Rotate in the row of the key, and confirm.
  2. Run the script again with the old key. Both lines now say "refused, the key is not valid": LumoAuth answers 401.
  3. Copy the new key from the page API key rotated successfully, set REPORT_API_KEY to it, and run the script again. The output is the same as before the rotation.

The API keys page shows when each key was last used, and from which address.

Limits​

  • You create, rotate and revoke a key in the portal. The Admin API has no endpoint for that.
  • A write scope does not include the read scope. A job that lists and changes users needs admin:users:read and admin:users:write.
  • A key acts for the person who created it. LumoAuth refuses a call when that person no longer holds the permission that the endpoint needs, whatever scopes the key has.
  • Send the key in X-API-Key, or as Authorization: ApiKey …. A key in Authorization: Bearer gets the answer 401.
  • A key works only in its own organization, and an organization has at most 25 active keys.