|
| 1 | +--- |
| 2 | +title: How to Secure API Keys Using Next.js Server Components |
| 3 | +--- |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +We can use [Next.js server components](https://nextjs.org/docs/app/building-your-application/rendering/server-components) to properly secure our API key from exposure in the frontend of our dapp. To further increase our API key security, we can also [restrict our API key to certain subgraphs or domains in Subgraph Studio](/cookbook/upgrading-a-subgraph/#securing-your-api-key). |
| 8 | + |
| 9 | +In this cookbook, we will go over how to create a Next.js server component that queries a subgraph while also hiding the API key from the frontend. |
| 10 | + |
| 11 | +### Caveats |
| 12 | + |
| 13 | +- Next.js server components do not protect API keys from being drained using denial of service attacks. |
| 14 | +- The Graph Network gateways have denial of service detection and mitigation strategies in place, however using server components may weaken these protections. |
| 15 | +- Next.js server components introduce centralization risks as the server can go down. |
| 16 | + |
| 17 | +### Why It's Needed |
| 18 | + |
| 19 | +In a standard React application, API keys included in the frontend code can be exposed to the client-side, posing a security risk. While `.env` files are commonly used, they don't fully protect the keys since React's code is executed on the client side, exposing the API key in the headers. Next.js Server Components address this issue by handling sensitive operations server-side. |
| 20 | + |
| 21 | +### Using client-side rendering to query a subgraph |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +### Prerequisites |
| 26 | + |
| 27 | +- An API key from [Subgraph Studio](https://thegraph.com/studio) |
| 28 | +- Basic knowledge of Next.js and React. |
| 29 | +- An existing Next.js project that uses the [App Router](https://nextjs.org/docs/app). |
| 30 | + |
| 31 | +## Step-by-Step Cookbook |
| 32 | + |
| 33 | +### Step 1: Set Up Environment Variables |
| 34 | + |
| 35 | +1. In our Next.js project root, create a `.env.local` file. |
| 36 | +2. Add our API key: `API_KEY=<api_key_here>`. |
| 37 | + |
| 38 | +### Step 2: Create a Server Component |
| 39 | + |
| 40 | +1. In our `components` directory, create a new file, `ServerComponent.js`. |
| 41 | +2. Use the provided example code to set up the server component. |
| 42 | + |
| 43 | +### Step 3: Implement Server-Side API Request |
| 44 | + |
| 45 | +In `ServerComponent.js`, add the following code: |
| 46 | + |
| 47 | +```javascript |
| 48 | +import React from "react"; |
| 49 | + |
| 50 | +const API_KEY = process.env.API_KEY; |
| 51 | + |
| 52 | +export default async function ServerComponent() { |
| 53 | + const response = await fetch( |
| 54 | + `https://gateway-arbitrum.network.thegraph.com/api/${API_KEY}/subgraphs/id/HUZDsRpEVP2AvzDCyzDHtdc64dyDxx8FQjzsmqSg4H3B`, |
| 55 | + { |
| 56 | + method: "POST", |
| 57 | + headers: { |
| 58 | + "Content-Type": "application/json", |
| 59 | + }, |
| 60 | + body: JSON.stringify({ |
| 61 | + query: ` |
| 62 | + { |
| 63 | + factories(first: 5) { |
| 64 | + id |
| 65 | + poolCount |
| 66 | + txCount |
| 67 | + totalVolumeUSD |
| 68 | + } |
| 69 | + } |
| 70 | + `, |
| 71 | + }), |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + const responseData = await response.json(); |
| 76 | + const data = responseData.data; |
| 77 | + |
| 78 | + return ( |
| 79 | + <div> |
| 80 | + <h1>Server Component</h1> |
| 81 | + {data ? ( |
| 82 | + <ul> |
| 83 | + {data.factories.map((factory: any) => ( |
| 84 | + <li key={factory.id}> |
| 85 | + <p>ID: {factory.id}</p> |
| 86 | + <p>Pool Count: {factory.poolCount}</p> |
| 87 | + <p>Transaction Count: {factory.txCount}</p> |
| 88 | + <p>Total Volume USD: {factory.totalVolumeUSD}</p> |
| 89 | + </li> |
| 90 | + ))} |
| 91 | + </ul> |
| 92 | + ) : ( |
| 93 | + <p>Loading data...</p> |
| 94 | + )} |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Step 4: Use the Server Component |
| 101 | + |
| 102 | +1. In our page file (e.g., `pages/index.js`), import `ServerComponent`. |
| 103 | +2. Render the component: |
| 104 | + |
| 105 | +```javascript |
| 106 | +import ServerComponent from './components/ServerComponent' |
| 107 | + |
| 108 | +export default function Home() { |
| 109 | + return ( |
| 110 | + <main> |
| 111 | + <ServerComponent /> |
| 112 | + </main> |
| 113 | + ) |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### Step 5: Run and Test Our Dapp |
| 118 | + |
| 119 | +Start our Next.js application using `npm run dev`. Verify that the server component is fetching data without exposing the API key. |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +### Conclusion |
| 124 | + |
| 125 | +By utilizing Next.js Server Components, we've effectively hidden the API key from the client-side, enhancing the security of our application. This method ensures that sensitive operations are handled server-side, away from potential client-side vulnerabilities. Finally, be sure to explore [other API key security measures](/cookbook/upgrading-a-subgraph/#securing-your-api-key) to increase your API key security even further. |
0 commit comments