Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Next.jsでAPIキーを安全に扱う方法
Search
yamatai12
December 12, 2025
200
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Next.jsでAPIキーを安全に扱う方法
yamatai12
December 12, 2025
More Decks by yamatai12
See All by yamatai12
Goのerror型がシンプルであることの恩恵について理解する
yamatai1212
1
410
AI時代の開発でも開発前の段取りの整理と振り返りを徹底したい 🧠
yamatai1212
0
190
PostgreSQLだと外部参照キーにデフォルトでインデックスが貼られていない
yamatai1212
1
420
顧客の言葉を、そのまま信じない勇気
yamatai1212
1
540
なぜPostgreSQLのGROUP BY句にエイリアスが使えるのか?
yamatai1212
0
230
SMTP完全に理解した ✉️
yamatai1212
0
420
DNS設定が必要になって初めて分かったこと
yamatai1212
0
130
LT登壇を続けたらポッドキャストに呼ばれた話
yamatai1212
0
1.3k
ページの可視領域を算出する方法について整理する
yamatai1212
0
310
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
1.1k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
600
Mobile First: as difficult as doing things right
swwweet
225
10k
Site-Speed That Sticks
csswizardry
13
1.6k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
570
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
460
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
870
Context Engineering - Making Every Token Count
addyosmani
9
1.3k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
580
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
340
Transcript
Next.jsでAPIキーを安全に扱う方法 yamatai12 1
自己紹介 yamatai12(Webエンジニア) SNS X(taiyama1212) Qiita(yamatai12) Zenn(yamatai12) 趣味は筋トレ プロフィールです、よろしくお願いします 2
今日のまとめ Next.jsのサーバーコンポーネントはサーバー上で実行される APIキーをサーバー側で付与すればブラウザに露出しない "server-only" で誤用を防げる 3
背景 あるNext.jsを使っているプロジェクトでは以下のような認可に関わる要素があった。 prod/dev 等の環境ごとに固定の共通のAPIキー APIキーをブラウザに公開されると、誰でもAPIを直接叩けてしまう。 4
Next.jsのサーバーコンポーネント (1/2) サーバー上で実行されるので、APIキーを安全に使える クライアントコンポーネント( 'use client' )は従来のReactと同じ 5
Next.jsのサーバーコンポーネント (2/2) //呟きに関する情報を表示するページ import LikeButton from "@/app/ui/like-button"; import { getTweet
} from "@/lib/data"; export default async function Page({ params, }: { params: { id: string }; }) { const { id } = params; const tweet = await getTweet(id); // サーバーコンポーネントでのデータ取得 return ( <div> <main> <h1>{tweet.title}</h1> {/* LikeButton から Server Action を呼んで “更新” する(mutation) */} <LikeButton tweetId={id} initialLikes={tweet.likes} /> {/* 従来のコンポーネント、stateやイベントハンドラー */} </main> </div> ); } 6
サーバーアクションでのデータフェッチ (1/3) Next.jsで使用できるサーバー上で実行される非同期関数のこと。 データの取得や変更をサーバー側で行える。 7
サーバーアクションでのデータフェッチ (2/3) "use client"; import { useState, useTransition } from
"react"; import { likeTweet } from "@/lib/actions"; export default function LikeButton({ tweetId, initialLikes, }: { tweetId: string; initialLikes: number; }) { const [likes, setLikes] = useState(initialLikes); const [isPending, startTransition] = useTransition(); return ( <button type="button" disabled={isPending} onClick={() => { startTransition(async () => { const nextLikes = await likeTweet(tweetId); // mutation(サーバーで更新) setLikes(nextLikes); }); }} ... 8
サーバーアクションでのデータフェッチ (3/3) "use server"; import { incrementTweetLike, getTweet } from
"@/lib/data"; // Server Action(mutation) export async function likeTweet(tweetId: string) { await incrementTweetLike(tweetId); // DB更新など(サーバー側) const tweet = await getTweet(tweetId); // 更新後の値を返す(簡易) return tweet.likes; } 9
APIキーをリクエストに付与するサーバー専用のモジ ュール 役割 サーバーアクションで実行するリクエストをインターセプトし、 リクエストの内容を変更する import "server-only" // ← これがあると、クライアントで誤ってimportするとビルドエラーになる
... axiosInstance.interceptors.request.use((config) => { config.baseURL = serverEnv.API_ORIGIN; config.headers["X-Api-Key"] = serverEnv.API_KEY; return config; }); ... //このaxiosInstanceを再exportする 10
Next.jsサーバーを通す構成 これにより、APIキーや環境変数をクライアントから見れないようにすることができる 11
まとめ Next.jsのサーバーコンポーネントはサーバー上で実行される APIキーをサーバー側で付与すればブラウザに露出しない "server-only" で誤用を防げる 12
参考 https://kaminashi-developer.hatenablog.jp/entry/nextjs-server-actions https://Next.js.org/docs/14/app/building-your-application/data-fetching/server- actions-and-mutations https://Next.jsjp.org/docs/app/getting-started/server-and-client-components https://qiita.com/buntafujikawa/items/78e9204cc9ea7eaabd3d 13