Google Pexel Photo Search Using Angular 8

Introduction

 
In this article, we will learn how to search images and vidoes from Google Pexel using its API in Angular 8. Anyone can download the images and the videos for free. 
 
The Pexels API provides a way for you to access all free videos and photos from Pexels.
 
You can interract with the API directly, or alternatively, use one of the language libraries. If you don't see a library for the language we use we can search Github for an unofficial one or write our own.
 
Prerequisites
  • Basic knowledge of Angular
  • Visual Studio Code must be installed
  • Angular CLI must be installed
  • Node JS must be installed
Google Pexel Image Search API.
 
To get Google Pexel API go to here.
 
 
Click on the Documentation; you can find the API there. 
 
 https://api.pexels.com/v1/search?query=nature&per_page=1
 
 

Get the API key

 
To get the API key for photo search click on the Authorizatiom menu and you will find a direct link to get your API key but for that you must need to login with your Google account.
 
 
Now API and authorization key is ready and it's time to create our Angular application. For that go to your project folder and create an Angualr application.
 

Create Angular project

 
Step 1
 
Create a new Angular project using the following NPM command:
 
ng new googleimagesearch
 
Step 2
 
Open the newly-created project in Visual Studio code and install Bootstrap in this project.
 
npm install bootstrap --save
 
Now, open styles.css file and add Bootstrap file reference. To add reference in styles.css file add this line.
  1. @import'~bootstrap/dist/css/bootstrap.min.css';   
Step 3
 
Now, let's create a new component, by using the following command.
 
ng g c imageSearch
 
Step 4

Now, open imageSearch.component.html file and add the following code in this file.
  1. <div class="card-header main-search">    
  2.   <div class="row">    
  3.     <div class="col-12 col-md-3 col-xl-3">    
  4.       <input [(ngModel)]="searchData" class="AutoFocus form-control" placeholder="Type something..." type="text">    
  5.     </div>    
  6.     <div class="col-12 col-md-3 col-xl-3">    
  7.       <input name="deliveryNumber" [(ngModel)]="perPage" class="AutoFocus form-control" placeholder="No of Images"    
  8.         type="text">    
  9.     </div>    
  10.     <div class="ml-auto">    
  11.       <input type="button" (click)="search()" value="Search" class="btn btn-primary search-btn">    
  12.     </div>    
  13.   </div>    
  14. </div>    
  15. <div class="container">    
  16.   <div class="row">    
  17.     <div *ngFor="let pic of photos" class="col-sm-4">    
  18.       <div style="margin-top: 10px;" class="card">    
  19.         <img class="card-img-top" [src]="pic.src.landscape" alt="Card image cap">    
  20.         <div class="card-body">    
  21.           <h5 class="card-title">Card title</h5>    
  22.           <a href="#" class="btn btn-primary">Know more</a>    
  23.         </div>    
  24.       </div>    
  25.     </div>    
  26.   </div>    
  27. </div>     
Step 5
 
The nextsStep is to open imageSearch.component.tsfile and paste the below code. 
  1. import { Component } from '@angular/core';  
  2. import { PexelPhotoSearchService } from '../pexel-photo-search.service';  
  3.   
  4. @Component({  
  5.   selector: 'app-image-search',  
  6.   templateUrl: './image-search.component.html',  
  7.   styleUrls: ['./image-search.component.css']  
  8. })  
  9. export class ImageSearchComponent {  
  10.   searchData;  
  11.   perPage: any;  
  12.   photos = [];  
  13.   constructor(private pexelPhotoSearchService: PexelPhotoSearchService) { }  
  14.   
  15.   search() {  
  16.     this.pexelPhotoSearchService.getdata(this.searchData, this.perPage).subscribe((response: any) => {  
  17.       console.log(response);  
  18.       this.photos = response.photos;  
  19.     }, (error) => {  
  20.       console.log(error);  
  21.     })  
  22.   }  
  23.   
  24. }  
In the above image SharingData is sending data using next as well as subsribed in the constructor to fetch the data in the data stream.
 
Step 6
 
Create a service where we can keep the Authorization key and search API.
  1. ng g s pexelphotosearch   
Next , open pexel-photo-search.service.ts and paste in that file.
  1. import { Injectable } from '@angular/core';  
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';  
  3. import { Observable,throwError } from 'rxjs';  
  4. import {catchError} from 'rxjs/operators';  
  5.   
  6. const httpOptions={  
  7.   headers : new HttpHeaders({  
  8.     'Authorization':'563492ad6f917000010000014060d806c66c47b88b9b4d7f8c487692'  
  9.   })  
  10. }  
  11.   
  12. @Injectable({  
  13.   providedIn: 'root'  
  14. })  
  15. export class PexelPhotoSearchService {  
  16.    
  17.   constructor(private http:HttpClient) { }  
  18.   
  19.   getdata(search,perPage):Observable<any>{  
  20.     const url="https://api.pexels.com/v1/search?query="+search+"&per_page="+perPage;  
  21.     return this.http.get<any>(url,httpOptions).pipe(catchError(this.handelError));  
  22.   }  
  23.   handelError(error){  
  24.     return throwError(error.message || "Server Error");  
  25.   }  
  26. }  
 
 
In the above image the API and Authorization key is the key factor in this application. It can fetch the image which you have searched. 
 
The last step is to give the selector of our component inside app.component.html file. 
  1. <app-image-search></app-image-search>  

Now we are done with all our coding part and it's  time for the output.

 
 
 
 

Summary

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


Similar Articles