Build Customer Dashboard
With the Consumer Portal, you can build in-app user-facing dashboards where your customers can follow their consumption in real-time. We provide the authentication layer and React hooks to fetch data from OpenMeter so you can focus on building your application.
Authentication
The Consumer Portal uses token authentication to grant access to your users to
their usage. The token is generated on your backend via OpenMeter SDK or API and
is valid for 24 hours or until you invalidate it. Every portal token belongs to
one specific subject
and can be limited to one or multiple meter
(s)
optionally.
Because a portal token belongs to a specific subject
, you can use it in your
frontend application to query usage; as long as you ensure tokens are not shared
between users.
- If you're using OpenMeter Cloud, you can directly call the portal meter query Cloud API in your web application.
- If you're using OpenMeter open source, you'll need to expose the portal meter
query endpoint (
/api/v1/portal/meters/{meterSlug}/query
) to your users.
How to generate a portal token
Portal tokens should be generated on your backend and passed to your frontend application. You can generate a portal token using the OpenMeter Cloud API or the OpenMeter SDK as the following:
Caution: Never share your Secret API Key with your customers or frontend application. Only portal tokens should be shared with your users.
Query Usage with React
Once you have a portal token, you can query usage in your front-end application.
OpenMeter Provider
The OpenMeter Provider is a React Context Provider that will handle your authentication. We recommend setting up this provider at the root of your pages where you want to use OpenMeter React hooks.
A full example for Next.js can be found here.
Check out the following example that fetches a portal token from your backend and passes it to the OpenMeter Provider.
Query Subject Usage
Once we have the OpenMeter Provider setup, we can use the useOpenMeter
hook to
get an OpenMeter client. Components using OpenMeter React hooks need to be a
child component of the <OpenMeterProvider>
.
Note how we didn't pass subject
to the React Hook as this is already handled
by the <OpenMeterProvider>
.
Limiting Portal Access
Portal tokens are restricted to a single subject
by design, but you can also
restrict them to one or multiple meters. This is useful if you have multiple
meters to track customer usage, but you don't want to give access to all of them
to your users.
For example, you can restrict a portal token to the tokens
meter as the
following: