This repository contains the source code for the blog post, "The Production-Ready TCG Search: Securing API Keys with Next.js Server Components".
This project demonstrates the modern, secure way to fetch data from a third-party API within a Next.js 15 application. It evolves our previous vanilla JS tutorial into a production-grade example that protects your secret API keys.
➡️ Read the full tutorial here: Link to Blog Post
This project uses a URL-driven, server-first data fetching pattern. A Client Component manages the UI and updates the URL's search parameters, which triggers the parent Server Component to re-render and securely fetch new data.
- Secure API Key Management: Storing and using secret keys in
.env.localso they are never exposed to the browser. - Data Fetching in Server Components: Using
async/awaitdirectly within a Server Component to fetch data. - URL State Management: Using Next.js's
useRouteranduseSearchParamshooks to drive application state. - Client & Server Composition: Correctly composing interactive Client Components (
'use client') within parent Server Components. - Disabling Data Cache: Using
{ cache: 'no-store' }to ensure real-time data from the API.
To get this project running on your local machine, follow these steps.
- Node.js v20 or later.
- A free JustTCG API key. You can get one from the JustTCG Dashboard.
- Clone this repository to your local machine:
git clone https://github.com/JustTCG/nextjs-tcg-search-tutorial.git
- Navigate into the project directory and install dependencies:
cd nextjs-tcg-search-tutorial npm install - Create a local environment file by copying the example:
cp .env.local.example .env.local
- Open the newly created
.env.localfile and replace"your_api_key_here"with your actual JustTCG API key. - Start the Next.js development server:
npm run dev
Open http://localhost:3000 in your browser to see the application.
JustTCG is the dedicated, simple, and reliable pricing API for Trading Card Games, built by collectors for developers. We provide accurate, lightning-fast data, specializing exclusively in TCGs to eliminate bloat and irrelevant information.
🔗 Website: https://justtcg.com
📚 API Documentation: https://justtcg.com/docs
