Skip to main content

Initialization

import { Mio } from '@mio-xyz/sdk';

const client = Mio.init({
  clientId: process.env.NEXT_PUBLIC_MIO_CLIENT_ID!,
  redirectUrl: `${process.env.NEXT_PUBLIC_APP_URL}/api/mio/callback`,
  exchangeTokenUrl: '/api/exchange-token'
});
Mio.init throws if you call it more than once per browser session. Use Mio.getInstance() (or useMio) everywhere after the initial call.

Config parameters

clientId
string
required
OAuth application ID from the Mio dashboard. Exposed to the browser.
redirectUrl
string
required
Absolute URL where Mio sends users back after the connection flow. Must match the redirect URL registered in the Mio dashboard.
exchangeTokenUrl
string
required
Backend endpoint that swaps authorization codes for tokens on behalf of the browser.

Methods

connect(): Promise<void>

Redirects the browser into the Mio connection flow hosted on the Mio dashboard. Use it inside a click handler.

handleMioCallback(): Promise<MioOauth2TokenResponse>

Exposed via useMio. Reads the code query parameter, clears it from the URL, forwards it to exchangeTokenUrl, and returns the parsed tokens.
response
MioOauth2TokenResponse
required
MioOauth2TokenResponse containing accessToken, refreshToken, expiresIn, tokenType, and idToken.

exchangeCode(code: string): Promise<MioOauth2TokenResponse>

Low-level helper that powers handleMioCallback. Call it manually if you want to parse the URL yourself.
code
string
required
Authorization code received from Mio after the user approves access.
response
MioOauth2TokenResponse
required
MioOauth2TokenResponse containing accessToken, refreshToken, expiresIn, tokenType, and idToken.

getContext({ query, accessToken }): Promise<string>

Retrieves Mio Context for a given user and query and returns the validated context string.
query
string
required
Natural language query used to shape the returned Mio Context.
accessToken
string
required
Token returned by handleMioCallback or your backend.
const response = await Mio.getInstance().getContext({
  query: 'Tell me about the user’s professional history',
  accessToken
});
"Spent 12 years in fintech, prefers async updates, and owns the ACME workspace."

getContextSummary({ accessToken }): Promise<string | null>

Fetches the latest stored Mio Context summary for the user. Returns null when no summary exists yet.
accessToken
string
required
Token returned by handleMioCallback or your backend.

Returns

Error handling

Mio.getInstance() throws if you skip Mio.init. Initialize inside a provider or at app bootstrap.
exchangeCode throws when the backend responds with a non-2xx status. Check that exchangeTokenUrl is reachable and that your backend logs show the real auth error.
getContext and getContextSummary throw a generic error message when the fetch returns a non-2xx code. Inspect your browser network panel to see if the access token expired (401) or if the payload was invalid (400).