Next.js
With the App Router, the API call and the chart can live in the same page: the fetch runs on the server, and only the result reaches the browser.
Help me add Towers reinsurance tower charts to this Next.js App Router project. 1. Read the Towers setup skill at https://towers.vinsa.dev/.well-known/agent-skills/towers/SKILL.md and follow its steps in order. 2. Follow the Next.js path: the API call in a server-only module, the chart in a page. 3. Ask me for my Towers API key rather than making one up, and keep it in a server-side env file that is not committed. 4. When you are done, tell me what you changed and anything still waiting on me.
-
Add your key to the environment
.env.local TOWERS_API_URL=https://towers-api.vinsa.devTOWERS_API_KEY=twr_...Do not prefix these with
NEXT_PUBLIC_. That prefix would ship the key to the browser. -
Install the package and import its styles
Terminal window npm install @vinsa/towersapp/layout.tsx import '@vinsa/towers/styles.css';import './globals.css';export default function RootLayout({ children }: { children: React.ReactNode }) {return (<html lang="en"><body>{children}</body></html>);} -
Write the server-side call
lib/towers.ts import 'server-only';import type { TowersResponse } from '@vinsa/towers';export async function buildTowers(program: unknown): Promise<TowersResponse> {const res = await fetch(`${process.env.TOWERS_API_URL}/api/v1/towers`, {method: 'POST',headers: {Authorization: `Bearer ${process.env.TOWERS_API_KEY}`,'Content-Type': 'application/json',},body: JSON.stringify(program),cache: 'no-store',});if (!res.ok) {const problem = await res.json().catch(() => null);throw new Error(problem?.message ?? `Towers API answered ${res.status}`);}return res.json();}The
server-onlyimport makes the build fail if this file is ever pulled into browser code. -
Draw the result in a page
app/programs/[id]/page.tsx import { Towers } from '@vinsa/towers';import { buildTowers } from '@/lib/towers';import { loadProgram } from '@/lib/programs'; // your own dataexport default async function ProgramPage({ params }: { params: Promise<{ id: string }> }) {const { id } = await params;const program = await loadProgram(id);const towers = await buildTowers(program);return (<main><h1>{program.program?.programName}</h1><Towers data={towers} /></main>);}<Towers />is a client component, and the response is plain JSON, so it can be passed across directly.
Reacting to clicks
Section titled “Reacting to clicks”Function props such as onSelect cannot be passed from a server component. Wrap the chart in a small client component of your own:
'use client';
import { useState } from 'react';import { Towers } from '@vinsa/towers';import type { TowersResponse } from '@vinsa/towers';
export function ProgramTowers({ towers }: { towers: TowersResponse }) { const [selected, setSelected] = useState<string | null>(null);
return ( <> <Towers data={towers} onSelect={(key) => setSelected(key)} /> {selected && <p>Selected layer: {selected}</p>} </> );}Caching
Section titled “Caching”The API always gives the same answer for the same request. If a program changes rarely, replace cache: 'no-store' with next: { revalidate: 300 }, or cache on your side keyed by the program’s version.