build an image gallery with next js 13 2 pexels api

Опубликовано: 26 Июль 2026
на канале: CodeLive
20
0

Download 1M+ code from https://codegive.com/cba950c
building an image gallery in next.js 13 using the pexels api is an excellent way to showcase your skills with react and api integration. below is a detailed tutorial that will guide you through the process step-by-step.

prerequisites
basic knowledge of react and next.js.
node.js and npm installed on your machine.
an account on pexels to get your api key.

steps to build the image gallery

1. setting up the next.js project

first, create a new next.js project using the following command in your terminal:

```bash
npx create-next-app@latest pexels-image-gallery
cd pexels-image-gallery
```

2. install axios

to make api requests, we’ll use axios. install it using npm:

```bash
npm install axios
```

3. get your pexels api key

1. go to [pexels](https://www.pexels.com/) and create an account if you don’t have one.
2. navigate to the pexels api section and generate your api key.

4. create a pexels api utility

create a new file in the `lib` directory to manage the api calls. create a folder named `lib` in the root of your project, and then create a file named `pexels.js`:

```javascript
// lib/pexels.js
import axios from 'axios';

const api_key = process.env.pexels_api_key; // store your api key in the .env file
const base_url = 'https://api.pexels.com/v1/';

export const fetchphotos = async (query) = {
const response = await axios.get(`${base_url}search?query=${query}&per_page=15`, {
headers: {
authorization: api_key,
},
});
return response.data.photos;
};
```

5. set up environment variables

create a `.env.local` file in the root of your project and add your pexels api key:

```
pexels_api_key=your_pexels_api_key
```

remember to replace `your_pexels_api_key` with your actual api key.

6. create the image gallery component

now, create a component to display the images. create a new folder named `components` and then create a file named `gallery.js`:

```javascript
// components/gallery.js
import react from 'react';

const gallery ...

#NextJS #ImageGallery #windows
Next.js 13
image gallery
Pexels API
React components
web development
photo gallery
dynamic images
API integration
responsive design
frontend development
JavaScript frameworks
user interface
image loading
SEO optimization
client-side rendering