Skip to Content
Integrate AssetEmbedded

Embed the UI

This guide continues from the overview. You need your OAuth credentials and the ID of a demo Business, which the Quickstart shows how to create.

By the end of this guide you will have an Asset component rendering live data from a demo Business in your app. The examples use Next.js (App Router), but the steps work in any React framework. The only Asset-specific pieces are a server endpoint that mints tokens and a provider that wraps your components.

Step 1: Install the packages

npm install @get-asset/sdk @get-asset/react

@get-asset/sdk provides the AssetProvider, and @get-asset/react provides the styled components. Both are public on npm, so no extra registry setup is needed.

Step 2: Add your credentials

Create .env.local in your project root:

# Server-only: never prefix these with NEXT_PUBLIC_
ASSET_CLIENT_ID=your_client_id
ASSET_CLIENT_SECRET=your_client_secret

# Use the sandbox while you build
NEXT_PUBLIC_ASSET_API_URL=https://api.sandbox.getasset.com

# The demo Business you created
NEXT_PUBLIC_ASSET_BUSINESS_ID=biz_xxxxxxxxxxxxxxxxxxxx

The API URL already includes https://. Next.js only reads .env.local at startup, so restart your dev server after editing it.

Step 3: Create a token endpoint

This route runs on your server and trades your credentials for an access token for one Business. Call the Get an Access Token endpoint, passing the Business and the scopes the token may use.

// app/api/asset-token/route.ts
export async function POST(req: Request) {
  const { business_id, scope } = await req.json();

  const body = new URLSearchParams({
    client_id: process.env.ASSET_CLIENT_ID!,
    client_secret: process.env.ASSET_CLIENT_SECRET!,
    grant_type: "client_credentials",
    scope,
  });
  // Leave it out when there is no Business yet, such as during onboarding
  if (business_id) body.set("business_id", business_id);

  const res = await fetch(
    `${process.env.NEXT_PUBLIC_ASSET_API_URL}/v0/auth/token`,
    {
      method: "POST",
      headers: { "Content-Type": "application/x-www-form-urlencoded" },
      body,
    },
  );

  if (!res.ok) {
    console.error("Asset token request failed:", res.status, await res.text());
    return Response.json({ error: "token_request_failed" }, { status: 502 });
  }

  const { access_token } = await res.json();
  return Response.json({ access_token });
}

This version trusts business_id and scope from the request, which is fine for a sandbox demo. Before you go to production, read the Business from your customer's session and choose the scopes on the server. Otherwise any visitor can request a token for any Business. See Keep the Business ID on your server.

Step 4: Wrap your app with the AssetProvider

The AssetProvider gives every Asset component below it the Business and a way to get tokens.

// app/dashboard/layout.tsx
"use client";

import { AssetProvider } from "@get-asset/sdk";

const BUSINESS_ID = process.env.NEXT_PUBLIC_ASSET_BUSINESS_ID!;

async function fetchAccessToken() {
  const res = await fetch("/api/asset-token", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      business_id: BUSINESS_ID,
      scope: "report:read business:read",
    }),
  });
  return (await res.json()).access_token;
}

export default function DashboardLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <AssetProvider
      baseUrl={process.env.NEXT_PUBLIC_ASSET_API_URL}
      businessId={BUSINESS_ID}
      accessToken={fetchAccessToken}
    >
      {children}
    </AssetProvider>
  );
}

A few details that matter:

  • Pass a function, not a token. The provider calls accessToken whenever it needs a token and asks for a new one shortly before the current one expires, or after a request fails with a 401. Define the function once, outside your component, so it stays the same between renders.
  • Set baseUrl. It defaults to the production API, so a sandbox integration must pass it.
  • Keep the scope narrow. A token can only do what its scopes allow. The revenue card in the next step only reads reports and the Business, so that is all it asks for.

Step 5: Add your first component

// app/dashboard/page.tsx
"use client";

import { AssetRevenueCard } from "@get-asset/react";
import "@get-asset/react/theme.css";
import "@get-asset/react/styles.css";

export default function DashboardPage() {
  return <AssetRevenueCard />;
}

Both CSS imports are required, or the component renders unstyled. The card fetches revenue for the current period and handles its own loading and error states.

Step 6: Run it

npm run dev

Open http://localhost:3000/dashboard. You should see the revenue card with data from your demo Business.

If something isn't right:

What you seeLikely cause
The card shows an error, and /api/asset-token returns 401The credentials are wrong, or belong to a different environment than the API URL
An error mentioning https://https://The API URL has the scheme twice. It belongs in the variable only
The token request succeeds but the card still errorsThe Business doesn't exist in this environment, or the token is missing a scope the component needs
The Business ID is undefined in the browserThe variable is missing the NEXT_PUBLIC_ prefix, or the dev server wasn't restarted
The card renders but looks unstyledOne of the CSS imports is missing
Edited .env.local but nothing changedRestart the dev server

Next Steps

Now that one component works, see Managing Businesses to let each of your customers create their own Business, and browse the Components for more to embed.

Questions? Reach out via our Contact form.

On this page