Skip to Content
Octopus Energy Packages

Configure the handler

Supply upstream credentials, report errors, and allow additional origins

After creating a handler route, configure it to supply upstream credentials, report handler failures, or accept requests from additional origins.

Authenticate upstream requests

An onRequest callback is optional for public operations, but required when upstream requests need authentication. Use onRequest to look up your application’s session and supply credentials.

Replace the route’s POST definition with the example below. It uses an application-specific getSession helper that returns a session with an upstream access token or null.

app/api/graphql/route.ts
import { createGraphQLHandler } from "@octopus-energy/graphql/handler";
import { getSession } from "#/lib/auth/session";

export const POST = createGraphQLHandler({
  endpoint: "https://api.oexx-kraken.energy/v1/graphql/",
  async onRequest(request) {
    const session = await getSession(request.headers);

    if (!session) {
      return {
        action: "reject",
        rejection: { status: 401, message: "Sign in required." },
      };
    }

    return {
      action: "continue",
      requestHeaders: { authorization: session.accessToken },
    };
  },
});

The callback runs after request validation. If you need to read the body before forwarding, read a clone of the request. Return one of these onRequest actions:

ActionBehavior
continueForward the operation with optional requestHeaders.
rejectReturn a GraphQL error with the supplied status, public message, and optional code.
respondReturn a supplied Response without calling upstream.

Browser cookies and authorization headers stay in your application unless you explicitly return them in requestHeaders. Every action also accepts responseHeaders, for example to renew application cookies.

Report handler errors

In a Next.js Route Handler, use after to report handler failures after sending the response. Add the onResponse option below to your existing handler configuration. Keep your onRequest callback. Use error.source to select a reporter for validation rejections, onRequest failures, or upstream failures.

app/api/graphql/route.ts
import { createGraphQLHandler } from "@octopus-energy/graphql/handler";
import { after } from "next/server";
import { reportOnRequestError, reportUpstreamError, reportValidationError } from "#/lib/reporting";

export const POST = createGraphQLHandler({
  endpoint: "https://api.oexx-kraken.energy/v1/graphql/",
  onResponse({ error, request, response }) {
    if (!error) return;

    if (error.source === "validation") {
      after(() => reportValidationError({ error, request, response }));
    } else if (error.source === "onRequest") {
      after(() => reportOnRequestError({ error, request, response }));
    } else if (error.source === "upstream") {
      after(() => reportUpstreamError({ error, request, response }));
    }
  },
});

Allow additional origins

By default, the handler checks the host and port in Origin against x-forwarded-host, falling back to host. Set allowedOrigins to accept additional origins:

export const POST = createGraphQLHandler({
  endpoint: "https://api.oexx-kraken.energy/v1/graphql/",
  allowedOrigins: ["https://app.example.com"],
});

Entries in allowedOrigins match the full origin, including its scheme and port.

Configure CORS separately

allowedOrigins controls which origins the handler accepts. Configure cross-origin resource sharing (CORS) separately in your application for cross-origin browser requests. Forwarded host headers must come from trusted infrastructure.

On this page