Authentication
Conduit provides outbound authentication for HTTP requests made by connectors and handlers. Auth is configured per-connector or as named configs stored in the database.
Outbound Auth Types
OutboundAuth is a discriminated union on the type field:
none
No authentication applied.
{ type: 'none' }
api_key
Sends an API key in a header. Defaults to Authorization: Bearer <key>.
{
type: 'api_key',
key: 'sk-abc123',
headerName: 'Authorization', // optional, default 'Authorization'
prefix: 'Bearer', // optional, default 'Bearer'. Set '' for no prefix
}
basic
HTTP Basic auth. Credentials are Base64-encoded automatically.
{
type: 'basic',
username: 'user',
password: 'pass',
}
oauth2
Client credentials flow. Tokens are fetched and cached automatically.
{
type: 'oauth2',
clientId: 'my-client',
clientSecret: 'secret',
tokenUrl: 'https://auth.example.com/token',
scopes: ['read', 'write'], // optional
authMethod: 'post_body', // optional: 'post_body' | 'basic_auth'
}
authMethod controls how credentials are sent to the token endpoint:
'post_body'(default):client_idandclient_secretin the POST body'basic_auth': credentials in theAuthorization: Basicheader
custom
Full control over request preparation.
{
type: 'custom',
prepare(request, ctx) {
const headers = new Headers(request.init.headers)
headers.set('X-Custom-Auth', 'my-token')
return { url: request.url, init: { ...request.init, headers } }
},
}
The prepare function receives a PreparedRequest ({ url, init }) and an OutboundAuthPrepareContext ({ fetch, tokenStore? }). Return the modified request.
Per-Connector Auth
Pass auth directly when registering a connector:
const app = createConduit({ db: './app.db' })
await app.connector(myConnector, {
auth: {
type: 'api_key',
key: process.env.SERVICE_API_KEY,
},
})
The connector’s setup function receives the auth as an AuthClient on ctx.auth:
const myConnector = defineConnector({
name: 'my-service',
version: '1.0.0',
setup(ctx) {
// ctx.auth is an AuthClient (or null if no auth configured)
return {
async start() {
const res = await ctx.auth!.request('/api/status')
console.log(await res.json())
},
}
},
})
Named Auth Configs
Store auth configurations in the ConduitDb and reference them by name. This keeps secrets out of code and allows runtime updates.
const app = createConduit({ db: './app.db' })
// Store a named config (typically done via API or admin setup)
app.db!.upsertAuthConfig({
name: 'github',
baseUrl: 'https://api.github.com',
authType: 'api_key',
authConfig: { key: process.env.GITHUB_TOKEN },
})
// Reference by name when registering a connector
await app.connector(myConnector, { auth: 'github' })
If a connector’s name matches an auth config name in the database, it is resolved automatically even without an explicit auth option.
Using Auth in Handlers
Access named auth configs in process handlers via ctx.auth(name):
app.process({
name: 'fetch-issues',
on: 'cron:hourly',
async run(ctx) {
const github = ctx.auth('github')
const res = await github.request('/repos/org/repo/issues')
const issues = await res.json()
return { success: true, payload: issues }
},
})
The AuthClient interface:
interface AuthClient {
request(path: string, init?: RequestInit): Promise<Response>
readonly baseUrl: string
readonly authType: 'none' | 'api_key' | 'basic' | 'oauth2'
}
request() resolves relative paths against baseUrl and applies authentication headers automatically.
Token Caching
OAuth2 tokens are cached by a SHA-256 hash of (tokenUrl, clientId, scopes, authMethod). Tokens are refreshed 60 seconds before expiry.
- With database: tokens are persisted in ConduitDb across restarts
- Without database: tokens are cached in memory for the process lifetime
See also: Building Connectors, Configuration.