Skip to Content
Octopus Energy Packages

Fetch Summary Data

Fetch the two-week summary window and derive MeasurementsSummary

Summary data uses the same fetching layer as EnergyOverview: resolve market supply points, fetch measurements for those supply points, then transform the normalized records. The difference is that the timeframe is fixed by Create the Shell rather than user-controlled.

What you'll build

By the end of this guide, EnergySummaryCard will fetch normalized measurements for the two-week summary window and derive a MeasurementsSummary primitive model from ordered matching groups.

Use ordered matching groups

EnergySummary can compare one or more grouped series. The most direct summary input is the ordered array returned by groupMeasurements(records, {matching}). Passing the resolved market supply points groups by supply point and preserves the order returned by the supply-point query.

Define the market mapping

The GraphQL API returns territory-specific market names. Map those names to the package utilities once in your app, then reuse the mapping wherever you fetch supply points.

import type { UtilityMarkets } from "@octopus-energy/measurements/market-supply-points";

const utilityMarkets: UtilityMarkets = {
  electricity: ["GBR_ELECTRICITY"],
  gas: ["GBR_GAS"],
};

These values are examples. Replace them with the market names used by your territory or product.

Fetch supply points

Start from the descriptors returned by the Market Supply Points API. They carry the market supply point ID and normalized utility used for fetching measurements and grouping summary records.

_components/energy-summary-card.tsx
import { useQuery } from "@tanstack/react-query";
import { marketSupplyPointsQueryOptions } from "@octopus-energy/measurements/react-query";

const marketSupplyPointsQuery = useQuery(
  marketSupplyPointsQueryOptions(client, {
    accountNumber,
    propertyId,
    utilityMarkets,
  }),
);

Fetch measurements for the summary window

Pass the resolved supply points and the fixed summary timeframe to the measurement query. The query is disabled until supply points are available.

_components/energy-summary-card.tsx
import { measurementsQueryOptions } from "@octopus-energy/measurements/react-query";

const measurementsQuery = useQuery(
  measurementsQueryOptions(client, {
    propertyId,
    marketSupplyPoints: marketSupplyPointsQuery.data,
    timeframe,
  }),
);

Consumption defaults

The measurements API has its own defaults for electricity reading direction. If your summary needs generation/export data, pass readingDirections to measurementsQueryOptions and include readingDirection in your matching groups when you derive the summary.

Create the primitive summary

Use groupMeasurements with the matching option, then pass the ordered groups to createMeasurementsSummary.

_components/energy-summary-card.tsx
import { groupMeasurements } from "@octopus-energy/measurements";
import { createMeasurementsSummary } from "@octopus-energy/measurements/summary";

const summary = createMeasurementsSummary(
  groupMeasurements(measurementsQuery.data, {
    matching: marketSupplyPointsQuery.data ?? [],
  }),
  {
    currencyFallback: "GBP",
    locale: "en-GB",
    timeframe,
  },
);

The returned summary is always a state object:

FieldUse
status"ready" when at least one entry is comparable, otherwise "empty"
entriesone entry per input MeasurementGroup, including empty entries
periods.weekBefore / periods.lastWeeksafe total period values and completeness, when totals can combine
deltassafe total deltas by metric, when totals can be compared

This guide only creates the primitive model. The next guide introduces role helpers for headline copy, indicators, chart bars, and chart labels.

Handle query states

For the starter version, render request errors separately from empty summary data. An empty summary means the request succeeded but the records cannot yet produce a comparable entry.

_components/energy-summary-card.tsx
import { Typography } from "@octopus-energy/coral/atoms";
import { EnergySummary } from "@octopus-energy/coral/molecules";

const loading = marketSupplyPointsQuery.isPending || measurementsQuery.isPending;
const errorMessage = marketSupplyPointsQuery.error?.message ?? measurementsQuery.error?.message;
const error = Boolean(errorMessage);

return (
  <EnergySummary
    error={error}
    errorMessage={errorMessage ?? "Unable to load your energy summary"}
    loading={loading}
  >
    <Typography textStyle="body2">
      {summary.status === "ready"
        ? "Summary data is ready to render."
        : "No summary data is available yet."}
    </Typography>
  </EnergySummary>
);

The next guide replaces the temporary body with formatted text, indicators, and charts.

Implementation checkpoint

At this checkpoint, EnergySummaryCard owns the fixed summary timeframe, fetches market supply points and measurements with TanStack Query, and derives a MeasurementsSummary primitive model for the Coral summary card.

FAQ

On this page