Skip to main content

Call any endpoint of the API

Reach an endpoint that the hand-written SDK does not wrap: with the generated client in JavaScript and Python, and with the request method of the SDK in PHP, Java and C#.

RecipeReadyView source
Works with
JavaScript / TypeScriptPythonPHPJavaC# / .NET

What it shows​

  1. The SDK gives you a client for the whole API, already set up with your base URL and API key.
  2. The program calls an Admin API endpoint that the hand-written SDK has no method for: the list of users, with a search text and a sort order.
  3. It reads the answer as typed data: each user, and the pagination object.
  4. It asks for the next page until hasNextPage is false.

Each language reaches the endpoint in its own way:

LanguageWhat the program uses
JavaScriptlumo.api.create(AdminUsersApi) on LumoAuthBackend. It needs the package @lumoauth/api-client.
PythonAdminUsersApi(lumo.api). It needs the package lumoauth-api-client, which pip install "lumoauth[api]" installs.
PHP$lumo->http()->request(...) with $lumo->http()->orgPath(...). The answer is an array.
Javalumo.request(method, path, body, type) with lumo.orgPath(...). The answer becomes your own record type.
C#lumo.RequestAsync<T>(method, path) with lumo.OrgPath(...). The answer becomes your own record type.

In every language the call sends the API key, and an error status becomes one of the typed errors of the SDK.

Before you start​

  • An API key with the scope admin:users:read. Create it in the portal under Applications & developer tools, API keys.
  • Three users to list. The setup script creates them. Its API key also needs admin:users:write.
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.
LUMOAUTH_API_KEYAn API key with the scope admin:users:read. The setup script also needs admin:users:write.
SEARCHOptional. The text that the email address, the name or the username of a user contains. Default: @contractors.example.com.

The setup script creates three test users at contractors.example.com, so that the list has two pages. You can run it again at any time.

node operations/any-endpoint/setup.mjs
setup.mjs
// Creates three test users with addresses at contractors.example.com, so that
// the list of the recipe has more than one page. Run it again at any time.
//
// node operations/any-endpoint/setup.mjs
//
// The API key needs the scope admin:users:write.
import { randomBytes } from 'node:crypto';
import { ensure, printEnv } from '../../_lib/admin.mjs';

for (const [email, name] of [
['ana.ortiz@contractors.example.com', 'Ana Ortiz'],
['ben.okafor@contractors.example.com', 'Ben Okafor'],
['chloe.park@contractors.example.com', 'Chloe Park'],
]) {
await ensure('/admin/users', (u) => u.email === email, {
email,
name,
// The recipe never signs in as these users, so nobody needs the password.
password: `${randomBytes(18).toString('base64url')}#Aa1`,
emailVerified: true,
});
}

console.log('Three users at contractors.example.com are ready.');
printEnv({ SEARCH: '@contractors.example.com' });

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. JavaScript and Python use the generated client through the SDK. PHP, Java and C# use the request method of the SDK.

operations/any-endpoint/js/list-users.mjs
// List users page by page with the generated client, which the SDK sets up
// with your credentials. The hand-written SDK has no call for this list.
//
// node operations/any-endpoint/js/list-users.mjs
//
// lumo.api needs the package @lumoauth/api-client next to @lumoauth/backend.
import { LumoAuthBackend } from '@lumoauth/backend';

const lumo = new LumoAuthBackend({
baseUrl: process.env.LUMOAUTH_URL,
orgId: process.env.LUMOAUTH_ORG_ID,
secretKey: process.env.LUMOAUTH_API_KEY,
});

// One class for each group of endpoints, with the base URL and the API key
// already set.
const { AdminUsersApi, ListUsersSortBy, ListUsersSortDir } = lumo.api.module;
const users = lumo.api.create(AdminUsersApi);

const search = process.env.SEARCH ?? '@contractors.example.com';
let total = 0;
for (let page = 1; ; page++) {
// In TypeScript the parameters have the type AdminUsersApiListUsersRequest,
// and the answer has the type ListUsersResponse. The answer is an axios
// response: the JSON is in its data field.
const { data: answer } = await users.listUsers({
orgId: process.env.LUMOAUTH_ORG_ID,
search,
sortBy: ListUsersSortBy.EMAIL,
sortDir: ListUsersSortDir.ASC,
page,
limit: 2,
});
console.log(`page ${page} of ${answer.pagination.totalPages}`);
for (const user of answer.data) console.log(` ${user.email} (${user.name})`);
total += answer.data.length;
if (!answer.pagination.hasNextPage) break;
}

console.log(`${total} users`);
node operations/any-endpoint/js/list-users.mjs

Check the result​

Output
page 1 of 2
ana.ortiz@contractors.example.com (Ana Ortiz)
ben.okafor@contractors.example.com (Ben Okafor)
page 2 of 2
chloe.park@contractors.example.com (Chloe Park)
3 users
  • The list has two pages because the program asks for two users on each page. Set SEARCH to another text to list other users.
  • The portal shows each request of the key in the audit log, under Security, Audit logs. The entry is api_key.used, and its actor is the name of the API key.
  • The Users reference lists every parameter of the list: active, blocked, emailVerified, role, group and others.

Limits​

  • In the generated clients, many calls that create or change something have no parameter for the request body. Examples are createUser, updateUser and adminGroupsCreate. For such a call, use the request method of the SDK, or plain HTTP, and send the JSON body that the API reference describes.
  • A page holds at most 100 users.
  • The search text is case-sensitive.