Documentation

Quickstart

Register an app, sign yourself in with it, call the API as yourself, then as your organization.

In this tutorial you register an app, sign in to it with your own Davi account, and call the API twice: once as yourself, and once on behalf of your organization. Everything runs from a terminal and a browser, with no server.

You need:

  • A Davi account that is an owner or manager of an organization, because apps are registered to an organization.
  • A terminal with curl and openssl.

The API is served from https://api.davi.social, and every request carries an OAuth 2.0 bearer token.

1. Register an app

In the Davi dashboard, open your organization and go to Developer → Apps → Create app.

  1. Choose Server-side application. This makes a confidential app, which has a client secret.
  2. Add the redirect URI http://localhost:8765/callback. Nothing needs to be listening there; you will read the code from the browser's address bar.
  3. Add the scopes user:read and org:list. openid needs no registration.

Create the app and copy the Client ID and Client Secret.

The client secret is shown only once. If you lose it, rotate it from the app's page.

Then, on the app's page, enable the Token exchange grant. A new app can only sign users in, and step 6 exchanges a token.

Set both values in your terminal:

CLIENT_ID="paste the client ID"
CLIENT_SECRET="paste the client secret"

2. Create a PKCE pair

Every sign-in uses PKCE: a random secret, the code_verifier, that only you know, and its hash, the code_challenge, that you send first.

CODE_VERIFIER=$(openssl rand -base64 48 | tr -d '=+/\n')
CODE_CHALLENGE=$(printf '%s' "$CODE_VERIFIER" | openssl dgst -sha256 -binary | openssl base64 | tr '+/' '-_' | tr -d '=\n')

3. Sign in

Print the authorization URL:

echo "https://davi.social/oauth/authorize?response_type=code&client_id=$CLIENT_ID&redirect_uri=http://localhost:8765/callback&scope=openid%20user:read%20org:list&state=quickstart&code_challenge=$CODE_CHALLENGE&code_challenge_method=S256"

Open it in a browser, sign in to Davi, and approve the consent screen. The browser then tries to load http://localhost:8765/callback and shows that it cannot connect. That is expected. The address bar reads:

http://localhost:8765/callback?code=AUTHORIZATION_CODE&state=quickstart

Copy the code value. It expires in 10 minutes and works once.

CODE="paste the code"

4. Exchange the code for tokens

curl -X POST "https://api.davi.social/oauth2/token" \
  -u "$CLIENT_ID:$CLIENT_SECRET" \
  -d "grant_type=authorization_code" \
  -d "code=$CODE" \
  -d "redirect_uri=http://localhost:8765/callback" \
  -d "code_verifier=$CODE_VERIFIER"
{
  "access_token": "eyJhbGciOi...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "def502...",
  "scope": "openid org:list user:read",
  "id_token": "eyJhbGciOi..."
}

Copy the access token:

ACCESS_TOKEN="paste the access_token"

5. Call the API as yourself

curl "https://api.davi.social/api/v1/users/me" \
  -H "Authorization: Bearer $ACCESS_TOKEN"

The response is your own account:

{
  "uuid": "0b6c…",
  "username": "ada",
  "first_name": "Ada",
  "last_name": "Lovelace",
  "email": "ada@example.com",
  "scopes": ["openid", "org:list", "user:read"],
  "…": "…"
}

Now list the organizations you belong to:

curl "https://api.davi.social/api/v1/organizations" \
  -H "Authorization: Bearer $ACCESS_TOKEN"

Every organization you are on the staff of is in items. Note the slug of the one you registered the app in.

ORG_SLUG="paste the slug"

6. Call the API as your organization

Writing to an organization, such as creating an activity or issuing a reward, needs a token that acts for that organization. Exchange your access token for one:

curl -X POST "https://api.davi.social/oauth2/token" \
  -u "$CLIENT_ID:$CLIENT_SECRET" \
  -d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange" \
  -d "subject_token=$ACCESS_TOKEN" \
  -d "subject_token_type=urn:ietf:params:oauth:token-type:access_token" \
  -d "resource=org:$ORG_SLUG"
ORG_TOKEN="paste the access_token from that response"

List organizations again, with the new token:

curl "https://api.davi.social/api/v1/organizations" \
  -H "Authorization: Bearer $ORG_TOKEN"

This time items holds one organization, the one the token acts for.

You have registered an app, signed in with it, and called the API both as a user and as an organization. Every integration is built from these two tokens.

Where to go next