This page shows you how to connect a user account to Mio, store tokens safely, and bring personalized Mio Context into your product. You will need Mio dashboard access, a redirect URL, Node.js 18+, and a server environment (Next.js route handler, Express API, etc.).
OAuth & token flow at a glance
- User clicks Connect with Mio and gets redirected to the Mio connection flow.
- Mio redirects back to your
redirectUrlwith a short-lived authorization code. - The browser posts that code to your backend exchange endpoint.
- The backend calls
mio.exchangeCodeForTokens(code), stores therefreshToken, and returns theaccessTokento the browser. - You call
getContextSummary(orgetContext) with the access token. - When the access token expires (after 24 hours), the backend calls
mio.refreshTokens(refreshToken)to mint a new one without asking the user to reconnect.
Ship a working flow in six steps
1
Register your app
Visit the Mio dashboard, create an OAuth client, and copy the
clientId, clientSecret, and redirect URL you intend to use locally. You need these values before any code changes.2
Use @mio-xyz/sdk as a project dependency
3
Configure environment variables
Provide Mio credentials on both the browser and the server. Prefix client-side values with
NEXT_PUBLIC_ (or your framework equivalent).4
Initialize Mio in your frontend
Initialize Mio once in your frontend so you can trigger the connection flow and later fetch Mio Context.
- React
- Vanilla HTML + JavaScript
app/providers.tsx
5
Create the token exchange endpoint
Your frontend can never store the Mio client secret, so spin up a backend endpoint that trades authorization codes for tokens and hands the short-lived
accessToken back to the browser.What is the exchange token endpoint for?
It is the bridge between the redirect and your stored tokens:- The browser posts the
codeit received from Mio to this endpoint. - The backend calls
mio.exchangeCodeForTokens(code)with your secret credentials. - You store the
refreshTokensecurely, send theaccessToken(or a session identifier) back to the browser, and use it to call Mio Context.
- Next.js route handler
- Express endpoint
app/api/exchange-token/route.ts
6
Fetch Mio Context & personalize your experience
Once you have an access token, call
getContextSummary to pull ready-to-use context. The React example below uses useMio; adapt the same pattern for other frameworks or use getContext when you need a custom prompt.- React
- Vanilla HTML + JavaScript
app/page.tsx
Run through the full flow, call
handleMioCallback, and confirm getContextSummary logs personalized content in the console.What just happened
- You registered a Mio OAuth client and installed the SDK.
- You configured environment variables and stood up a secure token exchange route.
- You wrapped your React tree with
MioProvider, handled the callback, and called bothgetContextandgetContextSummaryusing a real access token.
Keep building
- Need a high-level recap? Read the Overview.
- Ready for implementation details? Dive into the API reference and server helpers.
- Want type definitions? Check Types for every response and schema.