Getting started
Get started with Tempo ID in an app.
Overview
The Accounts SDK provides what an app needs to sign users in with Tempo ID, act onchain on Tempo, and call the Tempo API.
Demo
The demo signs in through Tempo ID's dialog, and pays 1 OUSD on Tempo testnet with the sign-in's access key, with no approval.
Quick prompt
To add the flow of the demo to an app, paste this prompt into a coding agent.
Read https://accounts.tempo.com/llms-full.txt. Then add Tempo ID sign-in to this app with the `accounts` package. Follow the "Getting started" steps of this app's platform.Walkthrough
Each tab builds the flow on one platform, on Tempo testnet.
The web example runs these steps in a React app.
Create the instance
Create one instance for the app with Accounts.create. Each sign-in requests an access key with these limits.
import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Sign the user in
useSession returns the session, and renders again when it changes. Without a session, dialog.signIn opens Tempo ID's sign-in in a dialog over the page.
import { accounts } from './tempo.config.js'
export function App() {
const { session } = accounts.useSession()
if (!session)
return (
<button onClick={() => accounts.dialog.signIn()} type="button">
Sign in with Tempo
</button>
)
return <p>Signed in as {session.user.email}</p>
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Send a payment
pay sends 1 OUSD from the user's Tempo Account. If the access key's limits hold the amount, the access key signs it, and the user approves nothing. Otherwise, Tempo ID's payment page asks the user to approve it.
import { accounts } from './tempo.config.js'
export function Send() {
return (
<button
onClick={async () => {
const { transaction } = await accounts.pay({
amount: '1',
to: '0x000000000000000000000000000000000000dEaD',
token: 'ousd',
})
console.log(transaction)
}}
type="button"
>
Send 1 OUSD
</button>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [react(), viem()],
})Render the button once the user signs in.
The cli example runs these steps as the commands of one CLI.
Create the instance
A CLI names its own client ID, cli: and a name, so it needs no registration. The instance saves the session to ~/.tempo/id/store.json, which only the user can read.
import { Accounts } from 'accounts'
import { device, viem } from 'accounts/plugins'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for 7 days.
accessKey: () => ({
expiry: '7d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
clientId: 'cli:acme-example',
plugins: [
device({
onCode: (code) =>
console.log(
`Open ${code.verificationUriComplete} and confirm the code ${code.userCode}.`,
),
}),
viem(),
],
})Sign the user in
The device plugin runs the OAuth device authorization grant. device.signIn passes a code to onCode, the user approves it in any browser, and the call returns the session.
import { accounts } from './tempo.config.js'
const { user } = await accounts.device.signIn()
console.log(`Signed in as ${user.email}.`)import { Accounts } from 'accounts'
import { device, viem } from 'accounts/plugins'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for 7 days.
accessKey: () => ({
expiry: '7d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
clientId: 'cli:acme-example',
plugins: [
device({
onCode: (code) =>
console.log(
`Open ${code.verificationUriComplete} and confirm the code ${code.userCode}.`,
),
}),
viem(),
],
})Send a payment
Each later run reads the session from the file, and the access key signs the transfer. accounts.pay resolves once the chain includes the transfer. Without a session, it throws Accounts.SignedOutError.
import { accounts } from './tempo.config.js'
const session = await accounts.session.get()
if (!session?.accessKey) throw new Error('Not signed in. Run `login`.')
// The access key acts for the user's Tempo Account.
console.log(`${session.user.email} ${session.accessKey.account}`)
const { transaction } = await accounts.pay({
amount: '1',
to: '0x000000000000000000000000000000000000dEaD',
token: 'ousd',
})
console.log(`Sent in ${transaction}.`)import { Accounts } from 'accounts'
import { device, viem } from 'accounts/plugins'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for 7 days.
accessKey: () => ({
expiry: '7d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
clientId: 'cli:acme-example',
plugins: [
device({
onCode: (code) =>
console.log(
`Open ${code.verificationUriComplete} and confirm the code ${code.userCode}.`,
),
}),
viem(),
],
})The mobile example runs these steps in an Expo app.
Set the scheme of the app
Tempo ID returns the user to the app through its custom scheme.
{
"expo": {
"scheme": "com.example.app"
}
}A custom scheme needs a registration with Tempo ID, which issues the app's client ID and names its redirect URI.
Configure Metro
Metro reads the TypeScript source of ox in place of its build, and the bundle fails. This resolver skips the source.
const { getDefaultConfig } = require('expo/metro-config')
const config = getDefaultConfig(__dirname)
config.resolver.resolveRequest = (context, name, platform) =>
context.resolveRequest(
name.startsWith('ox/')
? {
...context,
sourceExts: context.sourceExts.filter((ext) => ext !== 'ts'),
}
: context,
name,
platform,
)
module.exports = configThe file is CommonJS, as Expo's own template is. Expo's base TypeScript config excludes it, so the type check skips it.
Create the instance
The expo plugin opens Tempo ID in the system browser, and keeps the session in the device's secure store.
import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [
expo({ redirectUri: 'com.example.app:/callback' }),
react(),
viem(),
],
})Sign the user in
redirect.signIn opens Tempo ID in an authentication sheet, and returns the session. accounts.useSession restores it after a restart of the app.
import { Button, Text } from 'react-native'
import { accounts } from './tempo.config.js'
export function App() {
const { session } = accounts.useSession()
if (!session?.accessKey)
return (
<Button
onPress={() => accounts.redirect.signIn()}
title="Sign in with Tempo"
/>
)
return (
<Text>
Signed in as {session.user.email}, with the Tempo Account{' '}
{session.accessKey.account}
</Text>
)
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [
expo({ redirectUri: 'com.example.app:/callback' }),
react(),
viem(),
],
})Send a payment
The access key signs the transfer, so the user sees no prompt.
import { Button } from 'react-native'
import { accounts } from './tempo.config.js'
export function Send() {
return (
<Button
onPress={async () => {
const { transaction } = await accounts.pay({
amount: '1',
to: '0x000000000000000000000000000000000000dEaD',
token: 'ousd',
})
console.log(transaction)
}}
title="Send 1 OUSD"
/>
)
}import { Accounts } from 'accounts'
import { expo } from 'accounts/expo'
import { viem } from 'accounts/plugins'
import { react } from 'accounts/react'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
// The client ID of the app's registration.
clientId: 'example-mobile',
plugins: [
expo({ redirectUri: 'com.example.app:/callback' }),
react(),
viem(),
],
})Render the button once the user signs in.
The server runs the sign-in through a redirect to Tempo ID, and keeps each session with its tokens and access key. The browser holds only a session ID, in an HTTP-only cookie, so the page needs no instance. The backend example runs these steps in a Hono server.
Create the instance
The server's instance keeps each session in its store, with the backend plugin. The viem plugin sends transactions with each session's access key. The app's origin, in APP_URL, is its client ID.
import { Accounts, Store } from 'accounts'
import { backend, viem } from 'accounts/plugins'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
clientId: process.env.APP_URL,
plugins: [backend({ store: Store.memory() }), viem()],
})Serve the sign-in routes
accounts.handler serves the sign-in routes under /api/auth. A deployed origin names the callback route in the redirect_uris of its web app manifest.
import { Hono } from 'hono'
import { accounts } from './tempo.config.js'
const app = new Hono()
app.all('/api/*', (c) => accounts.handler.fetch(c.req.raw))
export default appimport { Accounts, Store } from 'accounts'
import { backend, viem } from 'accounts/plugins'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
clientId: process.env.APP_URL,
plugins: [backend({ store: Store.memory() }), viem()],
})| Route | Result |
|---|---|
GET /api/auth/login | Starts a sign-in, and redirects the browser to Tempo ID |
GET /api/auth/callback | Completes the sign-in, sets the session cookie, and redirects to returnTo |
POST /api/auth/logout | Ends the session, and deletes the session cookie |
Sign the user in
A link to the login route leaves the page for Tempo ID's sign-in. After the sign-in, the callback returns the browser to returnTo with the session cookie. session.get returns the session of the request's cookie, or undefined.
import { Hono } from 'hono'
import { html } from 'hono/html'
import { accounts } from './tempo.config.js'
const app = new Hono()
app.all('/api/*', (c) => accounts.handler.fetch(c.req.raw))
app.get('/', async (c) => {
const session = await accounts.session.get(c.req.raw)
if (session?.method !== 'tempo-id')
return c.html(
html`<a href="/api/auth/login?returnTo=/">Sign in with Tempo</a>`,
)
return c.html(html`<p>Signed in as ${session.user.email}</p>`)
})
export default appimport { Accounts, Store } from 'accounts'
import { backend, viem } from 'accounts/plugins'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
clientId: process.env.APP_URL,
plugins: [backend({ store: Store.memory() }), viem()],
})Send a payment
pay with session sends 1 OUSD with the access key of the request's session, so the user approves nothing. The server opens no payment page, so a payment above the access key's limits throws.
import { Hono } from 'hono'
import { csrf } from 'hono/csrf'
import { html } from 'hono/html'
import { accounts } from './tempo.config.js'
const app = new Hono()
app.all('/api/*', (c) => accounts.handler.fetch(c.req.raw))
// Another site cannot post the app's forms.
app.use(csrf())
app.get('/', async (c) => {
const session = await accounts.session.get(c.req.raw)
if (session?.method !== 'tempo-id')
return c.html(
html`<a href="/api/auth/login?returnTo=/">Sign in with Tempo</a>`,
)
return c.html(html`
<p>Signed in as ${session.user.email}</p>
<form action="/pay" method="post"><button>Send 1 OUSD</button></form>
`)
})
app.post('/pay', async (c) => {
const session = await accounts.session.get(c.req.raw)
if (session?.method !== 'tempo-id') return c.redirect('/')
const { transaction } = await accounts.pay({
amount: '1',
session,
to: '0x000000000000000000000000000000000000dEaD',
token: 'ousd',
})
return c.text(`Sent in ${transaction}.`)
})
export default appimport { Accounts, Store } from 'accounts'
import { backend, viem } from 'accounts/plugins'
export const accounts = Accounts.create({
// Each sign-in requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
clientId: process.env.APP_URL,
plugins: [backend({ store: Store.memory() }), viem()],
})Examples
Each example is a small app that installs as it is.
The examples folder holds the others.