Server requests and jobs
On the server, the signed-in user travels on each request as the X-TC-User header. Per-user SDK calls (tc.auth, tc.db.from, tc.db.asUser, tc.storage, tc.m365, tc.int, tc.teams) need that request. You can pass it explicitly or make it implicit.
Pass it explicitly
await tc.auth.user(req)
await tc.db.asUser(req).from('expenses').select('*')
await tc.m365.excel(url, { request: req }).table('Budget').rows()
await tc.storage.from('receipts', { request: req }).list()
await tc.int.salesforce.get('/query', { q }, { request: req })
req can be an Express/Node request, a Fetch Request, a Hono context, or the raw token string.
tc.middleware()
Connect/Express middleware that makes the current request implicit for the rest of the handler (it uses AsyncLocalStorage).
tc.middleware(): (req, res, next) => void
import express from 'express'
import { tc } from '@tenantcloud/sdk'
const app = express()
app.use(express.json())
app.use(tc.middleware())
app.get('/api/mine', async (req, res) => {
const { data } = await tc.db.from('expenses').select('*') // no req needed
res.json(data)
})
tc.withRequest(req, fn)
Runs fn with req as the current request. Use it with frameworks that don't take Connect middleware.
tc.withRequest<T>(req: RequestLike, fn: () => T): T
app.use('*', (c, next) => tc.withRequest(c, next))
export async function GET(request) {
return tc.withRequest(request, async () => Response.json(await tc.auth.user()))
}
Jobs act as the app
Cron and worker services (declared under services in tc.json) have no signed-in user. The platform injects TC_PROJECT_TOKEN, and when there's no request the SDK uses it automatically. Pass { as: 'project' } (Python: as_="project") to force the app identity.
As the app:
- Microsoft Graph is limited to the sites and files IT granted (Sites.Selected).
- Mail is sent from the project owner (
TC_PROJECT_OWNER_EMAIL, orfrom). - Storage calls manage every file in the project's buckets.
import { tc } from '@tenantcloud/sdk'
try {
const vendors = await tc.m365.list('https://contoso.sharepoint.com/sites/Procurement', 'Vendors').items()
for (const v of vendors) {
await tc.db.sql`insert into vendors (id, name) values (${v.id}, ${v.Title})
on conflict (id) do update set name = excluded.name`
}
} catch (err) {
if (err.code === 'ACCESS_PENDING') console.log(err.message) // re-run automatically once approved
else throw err
} finally {
await tc.db.end()
}
Errors
| Code | When |
|---|---|
NO_REQUEST | A per-user call with no request, outside middleware, and not in a job |
NO_PROJECT_IDENTITY | as: 'project' where TC_PROJECT_TOKEN isn't set (a web service) |
NO_SENDER | Mail as the app without an owner email |