Skip to content

Commit 7ca00e7

Browse files
ci: apply automated fixes
1 parent 834c38d commit 7ca00e7

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

docs/start/framework/react/fetching-external-api.md

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ id: fetching-external-api
33
title: Calling an external API using TanStack Start
44
---
55

6-
This guide demonstrates how to integrate external API calls into your TanStack Start application using route loaders. We will use TMDB API to fetch popular movies using TanStack Start and understand how to fetch data in a TanStack Start app.
7-
6+
This guide demonstrates how to integrate external API calls into your TanStack Start application using route loaders. We will use TMDB API to fetch popular movies using TanStack Start and understand how to fetch data in a TanStack Start app.
87

98
The complete code for this tutorial is available on [GitHub](https://github.com/shrutikapoor08/tanstack-start-movies).
109

11-
1210
## What You'll Learn
11+
1312
1. Setting up external API integration with TanStack Start
1413
1. Implementing route loaders for server-side data fetching
1514
1. Building responsive UI components with fetched data
@@ -21,8 +20,8 @@ The complete code for this tutorial is available on [GitHub](https://github.com/
2120
- Node.js and `pnpm` installed on your machine
2221
- A TMDB API key (free at https://themoviedb.org)
2322

24-
2523
## Nice to know
24+
2625
- [TanStack Router](https://tanstack.com/router/latest/docs/framework/react/routing/routing-concepts)
2726

2827
## Setting up a TanStack Start Project
@@ -48,6 +47,7 @@ pnpm dev
4847
## Understanding the Project Structure
4948

5049
At this point, the project structure should look like this:
50+
5151
```
5252
/movie-discovery
5353
├── src/
@@ -69,8 +69,8 @@ At this point, the project structure should look like this:
6969

7070
Once your project is set up, you can access your app at localhost:3000. You should see the default TanStack Start welcome page.
7171

72-
7372
## Step 1: Setup a .env file with TMDB_AUTH_TOKEN
73+
7474
To fetch movies from the TMDB API, you need an authentication token. You can get this for free at themoviedb.org.
7575

7676
First, let's set up environment variables for our API key. Create a `.env` file in your project root:
@@ -81,12 +81,12 @@ touch .env
8181
```
8282

8383
Add your TMDB API token to this file:
84+
8485
```
8586
TMDB_AUTH_TOKEN=your_bearer_token_here
8687
```
8788

88-
*Important*: Make sure to add `.env` to your `.gitignore` file to keep your API keys secure.
89-
89+
_Important_: Make sure to add `.env` to your `.gitignore` file to keep your API keys secure.
9090

9191
## Step 2: Defining Data Types
9292

@@ -114,37 +114,32 @@ export interface TMDBResponse {
114114
```
115115

116116
## Step 3: Creating the Route with API Fetch Function
117-
Now let's create our route that fetches data from the TMDB API. Create a new file at `src/routes/fetch-movies.tsx`:
118117

118+
Now let's create our route that fetches data from the TMDB API. Create a new file at `src/routes/fetch-movies.tsx`:
119119

120120
```typescript
121-
122121
// src/routes/fetch-movies.tsx
123122
import { createFileRoute } from '@tanstack/react-router'
124123
import type { Movie, TMDBResponse } from '../types/movie'
125124

126-
127-
const API_URL = 'https://api.themoviedb.org/3/discover/movie?include_adult=false&include_video=false&language=en-US&page=1&sort_by=popularity.desc'
125+
const API_URL =
126+
'https://api.themoviedb.org/3/discover/movie?include_adult=false&include_video=false&language=en-US&page=1&sort_by=popularity.desc'
128127

129128
async function fetchPopularMovies(): Promise<TMDBResponse> {
130-
const response = await fetch(
131-
API_URL,
132-
{
133-
headers: {
134-
'accept': 'application/json',
135-
'Authorization': `Bearer ${process.env.TMDB_AUTH_TOKEN}`,
136-
},
137-
}
138-
)
139-
129+
const response = await fetch(API_URL, {
130+
headers: {
131+
accept: 'application/json',
132+
Authorization: `Bearer ${process.env.TMDB_AUTH_TOKEN}`,
133+
},
134+
})
135+
140136
if (!response.ok) {
141137
throw new Error(`Failed to fetch movies: ${response.statusText}`)
142138
}
143-
139+
144140
return response.json()
145141
}
146142

147-
148143
export const Route = createFileRoute('/fetch-movies')({
149144
component: MoviesPage,
150145
loader: async () => {
@@ -157,7 +152,6 @@ export const Route = createFileRoute('/fetch-movies')({
157152
}
158153
},
159154
})
160-
161155
```
162156

163157
## Step 4: Building the Movie Components
@@ -211,6 +205,7 @@ const MovieDetails = ({ movie }: { movie: Movie }) => {
211205
```
212206

213207
## Step 5: Creating the MoviesPage Component
208+
214209
Finally, let's create the main component that consumes the loader data:
215210

216211
```jsx
@@ -233,20 +228,31 @@ const MoviesPage = () => {
233228
<h1 className="text-3xl mb-6 font-bold text-center">Popular Movies</h1>
234229

235230
{error && (
236-
<div className="text-red-400 text-center mb-4 p-4 bg-red-900/20 rounded-lg" role="alert" tabIndex={0}>
231+
<div
232+
className="text-red-400 text-center mb-4 p-4 bg-red-900/20 rounded-lg"
233+
role="alert"
234+
tabIndex={0}
235+
>
237236
{error}
238237
</div>
239238
)}
240239

241240
{movies.length > 0 ? (
242-
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6" aria-label="Movie List">
241+
<div
242+
className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6"
243+
aria-label="Movie List"
244+
>
243245
{movies.slice(0, 12).map((movie) => (
244246
<MovieCard key={movie.id} movie={movie} />
245247
))}
246248
</div>
247249
) : (
248250
!error && (
249-
<div className="text-center text-gray-400" role="status" tabIndex={0}>
251+
<div
252+
className="text-center text-gray-400"
253+
role="status"
254+
tabIndex={0}
255+
>
250256
Loading movies...
251257
</div>
252258
)
@@ -255,11 +261,10 @@ const MoviesPage = () => {
255261
</div>
256262
)
257263
}
258-
259264
```
260265

261-
262266
### Understanding How It All Works Together
267+
263268
Let's break down how the different parts of our application work together:
264269

265270
1. Route Loader: When a user visits `/fetch-movies`, the loader function runs on the server
@@ -268,15 +273,14 @@ Let's break down how the different parts of our application work together:
268273
4. Component Rendering: The `MoviesPage` component receives the data via `Route.useLoaderData()`
269274
5. UI Loads: The movie cards are rendered with the fetched data
270275

271-
272276
## Step 6: Testing Your Application
273-
Now you can test your application by visiting http://localhost:3000/fetch-movies. If everything is set up correctly, you should see a beautiful grid of popular movies with their posters, titles, and ratings. Your app should look like this:
274277

278+
Now you can test your application by visiting http://localhost:3000/fetch-movies. If everything is set up correctly, you should see a beautiful grid of popular movies with their posters, titles, and ratings. Your app should look like this:
275279

276280
![Netflix style movie setup](https://res.cloudinary.com/dubc3wnbv/image/upload/v1756512946/Screenshot_2025-08-29_at_5.14.26_PM_iiex7o.png)
277281

278-
279282
## Conclusion
283+
280284
You've successfully built a movie discovery app that integrates with an external API using TanStack Start. This tutorial demonstrated how to use route loaders for server-side data fetching and building UI components with external data.
281285

282-
While fetching data at build time in TanStack Start is perfect for static content like blog posts or product pages, it's not ideal for interactive apps. If you need features like real-time updates, caching, or infinite scrolling, you'll want to use TanStack Query on the client side instead. TanStack Query makes it easy to handle dynamic data with built-in caching, background updates, and smooth user interactions. By using TanStack Start for static content and TanStack Query for interactive features, you get fast loading pages plus all the modern functionality users expect.
286+
While fetching data at build time in TanStack Start is perfect for static content like blog posts or product pages, it's not ideal for interactive apps. If you need features like real-time updates, caching, or infinite scrolling, you'll want to use TanStack Query on the client side instead. TanStack Query makes it easy to handle dynamic data with built-in caching, background updates, and smooth user interactions. By using TanStack Start for static content and TanStack Query for interactive features, you get fast loading pages plus all the modern functionality users expect.

0 commit comments

Comments
 (0)