Skip to main content

Machine-to-machine access

Use sdk.asAdmin() for backend jobs: syncs, imports, scheduled tasks, integrations. No user signs in. The app authenticates with its own credentials.

import sdk, { initScenid } from '@scenid/cloud-sdk'

initScenid(serviceKey)

const admin = sdk.asAdmin()
// admin.Customers() admin.Insights() admin.Files() admin.Auth()

sdk.asApp() is an identical alias.

What happens under the hood​

  1. On the first call the SDK sends POST /oauth/token with grant_type: client_credentials, your client_id and client_secret.
  2. It receives an access token valid for one hour and caches it per client_id, refreshing 30 seconds before expiry.
  3. Each request carries Authorization: Bearer <token>.

You never handle the token yourself. It also works with the REST API directly.

What a service token may do​

ServiceAccess
CustomersEverything, all entities. Group ACLs do not apply
InsightsAll sources. Also the only token that may push to push sources
FilesUpload
AuthList users and user groups

Journal entries written this way have the author SYSTEM.

Example: nightly sync job​

sync.mjs
import sdk, { initScenid } from '@scenid/cloud-sdk'

initScenid(JSON.parse(process.env.SCENID_SERVICE_KEY))

const customers = sdk.asAdmin().Customers()

const { status, result } = await customers.Entities.list({
entityTypes: ['person'],
fields: ['data.firstName', 'data.lastName', 'data.contacts'],
hideInactive: true
})

if (status !== 200) throw new Error(`list failed: ${status}`)

console.log(`${result.length} active people`)

Run it from cron, a CI job or a serverless function.

Listing users and groups​

The Auth() service is available to admin contexts:

const auth = sdk.asAdmin().Auth()

const { result: users } = await auth.Users.list()
const { result: groups } = await auth.UserGroups.list()

See Auth admin.

Do not use this for end users​

A service token has no user identity and full tenant access. If people sign in to your product, use the user login flow so Scenid applies each person's permissions.