Google Pexel Photo Search Using ReactJS

Introduction

 
In this article, we will learn how to search images and vidoes from Google Pexel using its API in ReactJs. Anyone can download the images and the videos for free without any copyright issue.
 
The Pexels API provides a way to access all free videos and images from Google.
 
Prerequisites
  • Basic knowledge of ReactJs
  • Visual Studio Code must be installed
  • Node JS must be installed

Google Pexel Image Search API

 
To get the Google Pexel API go to here.
 
 
Click on the Documentation where you can find the API there.
 
 
Get the API key.
 
To get the API key for image search click on the Authorization menu and you will find a direct link to get your API key. For that you need to login with your Google account.
 
 
Now API and authorization key are ready and it's time to create our React application. For that go to your project folder and create a React application.
 

Create ReactJs project

 
Step 1
 
Now create a new React.js project by using the following command.
  1. npx create-react-app googleimagesearch
Step 2
 
Open the newly created project in Visual Studio code and install react bootstrap in this project by using the following command.
  1. npm install react-bootstrap --save 
Step 3
 
Now, open the index.js file and add import Bootstrap.
  1. import 'bootstrap/dist/css/bootstrap.min.css'; 
Step 4
 
Now Install the Axios library by using the following command.
 
Axios is to get th http request coming from server. 
  1. npm install --save axios 
Step 5
 
Its time to add our app.js code by using the following command. 
  1. import React from 'react'  
  2. import GooglePexel from "./googlePexel";  
  3.   
  4. function App() {  
  5.   return (  
  6.     <GooglePexel></GooglePexel>  
  7.   )  
  8. }  
  9.   
  10. export default App;  
Step 6
 
Create a new file googlepexel.js and add our frontend code using the following command. 
  1. import React, { useState } from "react";  
  2. import axios from 'axios';  
  3. import { Card } from 'react-bootstrap';  
  4. function googlePexel() {  
  5.     const [search, setSearch] = useState("");  
  6.     const [perPage, setPerPage] = useState("");  
  7.     const [result, setResult] = useState([]);  
  8.   
  9.     function handleChange(event) {  
  10.         const search = event.target.value;  
  11.         setSearch(search);  
  12.     }  
  13.     function noOfPics(event) {  
  14.         const perPage = event.target.value;  
  15.         setPerPage(perPage);  
  16.     }  
  17.   
  18.     function handleSubmit(event) {  
  19.         event.preventDefault();  
  20.         const url = "https://api.pexels.com/v1/search?query=" + search + "&per_page=" + perPage;  
  21.         const access_token = '563492ad6f917000010000014060d806c66c47b88b9b4d7f8c487692';  
  22.         axios.get(url, {  
  23.             headers: {  
  24.                 'Authorization': `${access_token}`  
  25.             }  
  26.         }).then(data => {  
  27.             console.log(data);  
  28.             setResult(data.data.photos);  
  29.         })  
  30.   
  31.     }  
  32.     return (  
  33.         <form onSubmit={handleSubmit}>  
  34.             <div className="card-header main-search">  
  35.                 <div className="row">  
  36.                     <div className="col-12 col-md-3 col-xl-3">  
  37.                         <input onChange={handleChange} className="AutoFocus form-control" placeholder="Type something..." type="text" />  
  38.                     </div>  
  39.                     <div className="col-12 col-md-3 col-xl-3">  
  40.                         <input onChange={noOfPics} name="deliveryNumber" className="AutoFocus form-control" placeholder="No of Images"  
  41.                             type="text" />  
  42.                     </div>  
  43.                     <div className="ml-auto">  
  44.                         <input type="submit" value="Search" className="btn btn-primary search-btn" />  
  45.                     </div>  
  46.                 </div>  
  47.             </div>  
  48.             <div class="container">  
  49.                 <div class="row">  
  50.                     {result.map(search => (  
  51.                         <div className="col-sm-4">  
  52.                             <Card style={{ 'margin-top''10px' }}>  
  53.                                 <Card.Img variant="top" src={search.src.landscape} alt={search.photographer} />  
  54.                                 <Card.Body>  
  55.                                     <h5 className="card-title">Card title</h5>  
  56.                                     <a className="btn btn-primary">Know more</a>  
  57.                                 </Card.Body>  
  58.                             </Card>  
  59.                         </div>  
  60.                     ))}  
  61.                 </div>  
  62.             </div>  
  63.         </form>  
  64.     )  
  65. }  
  66.   
  67. export default googlePexel  
 
Now we are done with all our coding part and it's time for the output.
 
In the below image we had searched for Bikes with 10 images.
 
 
In the below image we had searched for Mercedes with 100 images. 
 
 
In the below image we had searched for cars with 40 images. 
 
 
In the below image we searched for Laptops with 40 images.
 
 

Summary

 
In this article, we learned how to find any image using Google Pexel API.
 
Not only that, but we can also search the videos through Google Pexel API here.