From Blueprint Auth
Use createGraphQLRequest with your existing Blueprint Auth setup
Replace your GraphQL client calls with createGraphQLRequest while keeping your existing Blueprint Auth configuration, sessions, and GraphQL route.
Send requests from the browser
Point the client at your existing Blueprint Auth route. The browser sends the same-origin session cookie, and the route attaches the upstream credentials.
import { createGraphQLRequest } from "@octopus-energy/graphql/request";
const request = createGraphQLRequest("/api/graphql");
const data = await request(AccountQuery, { variables: { accountNumber } });Call Kraken directly from server code
Blueprint Auth’s getGraphQLClient.user attaches the token from the supplied auth context. After resolving and checking the context:
const userClient = getGraphQLClient.user({ auth: userAuth });
const viewer = await userClient.request(ViewerQuery);To migrate, keep the getAuth calls, create one request function for Kraken, and pass the resolved headers on each call.
import { createGraphQLRequest } from "@octopus-energy/graphql/request";
import { krakenHeaders } from "#/lib/auth/kraken-headers";
import { getAuth } from "#/lib/auth/server";
import { ViewerQuery } from "#/lib/graphql/queries";
const request = createGraphQLRequest("https://api.oexx-kraken.energy/v1/graphql/");
export async function getViewer() {
const auth = await getAuth.user();
if (!auth) throw new Error("Sign in required.");
return request(ViewerQuery, {
headers: krakenHeaders(auth),
});
}Build upstream headers
This helper accepts auth contexts returned by getAuth.viewer(), getAuth.user(), and getAuth.org(). Check for null before passing results from getAuth.user() or getAuth.org().
import type { AuthContext } from "@krakentech/blueprint-auth/server";
import { convertRecordToIterable } from "@krakentech/blueprint-auth/utils";
export function krakenHeaders(
auth: AuthContext.User | AuthContext.Unauthenticated | AuthContext.Org,
): Headers {
const headers = new Headers();
if (auth.headers) convertRecordToIterable(auth.headers, headers);
if (auth.accessToken) headers.set("authorization", auth.accessToken);
const clientIpSecret = process.env.KRAKEN_X_CLIENT_IP_SECRET_KEY;
if (auth.clientIp && clientIpSecret) {
headers.set("x-kraken-client-ip", auth.clientIp);
headers.set("x-kraken-client-ip-authorization", Buffer.from(clientIpSecret).toString("base64"));
}
return headers;
}The helper uses the same KRAKEN_X_CLIENT_IP_SECRET_KEY environment variable as Blueprint Auth. If your app sets the secret through krakenConfig.xClientIpSecretKey, use that server-side value instead.
Catch request failures
Replace ClientError checks with RequestError. The HTTP status stays available at error.response.status.
Before:
import { ClientError } from "graphql-request";
try {
await userClient.request(ViewerQuery);
} catch (error) {
if (!(error instanceof ClientError)) throw error;
console.error(error.message, error.response.status);
}After:
import { RequestError } from "@octopus-energy/graphql/request";
try {
await request(ViewerQuery, { headers: krakenHeaders(auth) });
} catch (error) {
if (!(error instanceof RequestError)) throw error;
console.error(error.message, error.response.status);
}For partial results, see Choose an error policy.