<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Karthik_B</title>
    <description>The latest articles on DEV Community by Karthik_B (@_iam_karthik).</description>
    <link>https://dev.to/_iam_karthik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F342613%2F5acb9a51-0023-4494-8af2-23795bc60ee7.jpg</url>
      <title>DEV Community: Karthik_B</title>
      <link>https://dev.to/_iam_karthik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_iam_karthik"/>
    <language>en</language>
    <item>
      <title>Getting started with Serverless Functions using Vercel — II</title>
      <dc:creator>Karthik_B</dc:creator>
      <pubDate>Wed, 20 Jul 2022 18:41:17 +0000</pubDate>
      <link>https://dev.to/playfulprogramming/getting-started-with-serverless-functions-using-vercel-ii-4jo7</link>
      <guid>https://dev.to/playfulprogramming/getting-started-with-serverless-functions-using-vercel-ii-4jo7</guid>
      <description>&lt;p&gt;&lt;strong&gt;[May 22, 2022]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hey all!&lt;br&gt;&lt;br&gt;
This is a continuation of the previous post on serverless functions. In this post, I have changed the approach slightly on how we can get country flag data and consume it as a user.&lt;br&gt;&lt;br&gt;
Check my previous blog post before going forward :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/timeless/getting-started-with-serverless-functions-using-vercel-i-868ab649fdce" rel="noopener noreferrer"&gt;Getting started with Serverless Functions using Vercel — I&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us get started.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hnl9on4tx0gyhps3ld1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2hnl9on4tx0gyhps3ld1.png" alt="Cover Image" width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the previous post, we had created serverless functions which were consumed using Axios POST request.&lt;/p&gt;

&lt;p&gt;So I thought, why not use it as a GET request and serve the image as a response.&lt;/p&gt;

&lt;p&gt;That would make the API something like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;/api/getCountryFlagByNationality?nationality=Australian&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;and we get a response like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6o8gcs7jcehb8m17p17.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6o8gcs7jcehb8m17p17.png" alt="Image of Flag" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pretty simple right?&lt;/p&gt;

&lt;p&gt;But the thing is… &lt;strong&gt;I have no idea where to get started.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5nmsjjdidacm5t8q848f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5nmsjjdidacm5t8q848f.jpg" alt="MEME-1" width="460" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So let me do some googling and get to know about it.&lt;/p&gt;

&lt;p&gt;Two things I need to know from what I see are:&lt;/p&gt;



&lt;ol&gt;
&lt;li&gt; Getting the query params from the URL itself&lt;/li&gt;
&lt;li&gt; Serve images as a response&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F08dve9u0lpi53vqi8w61.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F08dve9u0lpi53vqi8w61.jpg" alt="MEME-2" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famsuuys4u6a63nlbuykf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Famsuuys4u6a63nlbuykf.png" alt="MEME-3" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;[June 11, 2022]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I happen to remember from my subconscious memory 😅 that one of the open-source repo of one of my colleagues (&lt;a href="https://github.com/anuraghazra/github-readme-stats" rel="noopener noreferrer"&gt;Anurag Hazra's github-readme-stats&lt;/a&gt;) served SVG as a response.&lt;/p&gt;

&lt;p&gt;So I happen to look at the code and get a faint idea.&lt;/p&gt;

&lt;p&gt;The thing was in his open-source he has created an SVG and added details.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jotk0m2wn7i0gv46rs0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0jotk0m2wn7i0gv46rs0.png" alt="OS Reference" width="246" height="234"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What I had was a link to the image as an SVG.&lt;/p&gt;

&lt;p&gt;There are some steps to get it to work. Let me take you through this.&lt;/p&gt;

&lt;p&gt;Let us assume a case we need a flag based on nationality.&lt;/p&gt;

&lt;p&gt;Now, as we are passing the param in the URL. You can get the variable from &lt;code&gt;req.query&lt;/code&gt; your serverless function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { nationality } = _req_.query;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We will get our image URL from the library by doing this&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const image_url = findFlagUrlByNationality(nationality);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We need a help of a tiny library GOT (not Game of Thrones) 😜&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.npmjs.com/package/got" rel="noopener noreferrer"&gt;Checkout the GOT package here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is a powerful HTTP request library that will help to get our image of the flag.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const imageRequest = got(image_url);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This returns a Promise, which we can resolve to get the image and the buffer.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
const [imageResponse, imageBuffer] = 
     await Promise.all([imageRequest, imageRequest.buffer()]);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;With this, we construct the response of our serverless function.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Set the headers, add &lt;code&gt;cache-control&lt;/code&gt; &amp;amp; &lt;code&gt;content-type&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
res.setHeader("Cache-Control", "s-maxage=43200");

res.setHeader("content-type", imageResponse.headers["content-type"]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;Send the Image Buffer as a response
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_res_.send(imageBuffer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This wraps our serverless function. Let us try how it works.&lt;/p&gt;

&lt;p&gt;Open Chrome and head to&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[http://localhost:3000/api/getCountryFlagByNationality?nationality=Australian](http://localhost:3000/api/getCountryFlagByNationality?nationality=Australian)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This would now return the Flag of Australia as a response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67ljmn0mrg2rf4jpwi0m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F67ljmn0mrg2rf4jpwi0m.png" alt="Flag of Australia" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Woah! 💥&lt;/p&gt;

&lt;p&gt;Let us deploy it, and there you go; you have the SVG being served using a GET request.&lt;/p&gt;

&lt;p&gt;With this, you can pass our serverless function API directly to the src of HTML &lt;a href="" class="article-body-image-wrapper"&gt;&lt;img&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwk9bs7rp8iy1jdt17rfs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwk9bs7rp8iy1jdt17rfs.png" alt="Example how to use the new API" width="800" height="331"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click &lt;a href="https://country-flags-topaz.vercel.app/api/getCountryFlagByISO2Code?iso2code=AU" rel="noopener noreferrer"&gt;here&lt;/a&gt; to see the image of the Australia Flag.&lt;/p&gt;

&lt;p&gt;The serverless functions have been deployed in the URL :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://country-flags-topaz.vercel.app/api/getCountryFlagByISO2Code?iso2code=AU" rel="noopener noreferrer"&gt;https://country-flags-topaz.vercel.app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Usage:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;




&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;p&gt;Check out other examples &lt;a href="https://github.com/timelessco/country-flags#readme" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Head to the &lt;a href="https://github.com/timelessco/country-flags" rel="noopener noreferrer"&gt;repo&lt;/a&gt; and deploy an instance for yourself :)&lt;/p&gt;

&lt;p&gt;That brings us to the end of this post.&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This is&lt;/em&gt; &lt;a href="https://twitter.com/_iam_karthik" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;Karthik&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;em&gt;from&lt;/em&gt; &lt;a href="http://timeless.co/" rel="noopener noreferrer"&gt;&lt;em&gt;Timeless&lt;/em&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you find this blog post helpful, tell me in the comments would love to know your views. Thank you :)&lt;/p&gt;

&lt;p&gt;If you find any difficulties or see anything which could be done better, please feel free to add your comments!&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Getting started with Serverless Functions using Vercel — I</title>
      <dc:creator>Karthik_B</dc:creator>
      <pubDate>Wed, 20 Jul 2022 18:41:01 +0000</pubDate>
      <link>https://dev.to/playfulprogramming/getting-started-with-serverless-functions-using-vercel-i-52o7</link>
      <guid>https://dev.to/playfulprogramming/getting-started-with-serverless-functions-using-vercel-i-52o7</guid>
      <description>&lt;p&gt;Hey all!&lt;/p&gt;

&lt;p&gt;I worked on a simple side project where I had the need for Country Flags and came across an npm library which provided the data based on&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Country Name&lt;/li&gt;
&lt;li&gt; Nationality&lt;/li&gt;
&lt;li&gt; iso2Code&lt;/li&gt;
&lt;li&gt; iso3Code&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This library provides functions that return the required data.&lt;/p&gt;

&lt;p&gt;You can check the Github repo &lt;a href="https://github.com/ronatskiy/country-flags-svg" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So I thought why can't I just wrap each functionality around a serverless function and deploy it in Vercel.&lt;/p&gt;

&lt;p&gt;I won't be dealing with any detailed working of a serverless function here. I feel this blog is just a head start to get started around serverless function.&lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;p&gt;Creating a new Next.js App. The doc recommends using &lt;code&gt;create-next-app&lt;/code&gt; which sets up everything for us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the installation is complete, run &lt;code&gt;npm run dev&lt;/code&gt; to start the development server.&lt;/p&gt;

&lt;p&gt;Our application would run in &lt;code&gt;http://localhost:3000&lt;/code&gt; , let us visit it to view our application.&lt;/p&gt;

&lt;p&gt;I have changed &lt;code&gt;pages/index.js&lt;/code&gt; , to make the initial screen simple which renders just a Welcome and kept the rest of the code the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwar9et3hk50bg3da8r6t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwar9et3hk50bg3da8r6t.png" alt="index.js file of project" width="800" height="670"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With that done, let's get into Serverless Functions.&lt;/p&gt;

&lt;p&gt;So basically a serverless function is a chunk of code, a function, that takes a request and gets us a response.&lt;/p&gt;

&lt;p&gt;It can be written in different backend languages.&lt;/p&gt;

&lt;p&gt;I have chosen that language to be Javascript, the mother of all languages. 🔥&lt;/p&gt;

&lt;p&gt;Next.js supports serverless functions by default wherein you can add your exported functions into the &lt;code&gt;/api&lt;/code&gt; directory of our project root, &lt;code&gt;pages/api&lt;/code&gt; directory to be more precise when using Next.js.&lt;/p&gt;

&lt;p&gt;The basic structure of a Serverless Function is something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47hux1u9xc60h7wwlm8v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F47hux1u9xc60h7wwlm8v.png" alt="structure of serverless function" width="800" height="439"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any query or params we send would be inside the request object. We will get to it soon.&lt;/p&gt;

&lt;p&gt;Using the request we can perform any operation and return the response.&lt;/p&gt;

&lt;p&gt;Let me take the simplest example, the Country Flags library has a function that returns all the countries and their data.&lt;/p&gt;

&lt;p&gt;Create a new file in &lt;code&gt;pages/api&lt;/code&gt; a directory called &lt;code&gt;getAllCountriesData.js&lt;/code&gt; and add the below code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { countries } from "country-flags-svg";

export default function handler(_req_, _res_) { 

  _res_.status(200).json({ countries });

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now in &lt;code&gt;pages/index.js&lt;/code&gt; file, let us use the above function.&lt;/p&gt;

&lt;p&gt;This is a simple network request. I am using Axios. Let us create an async function&lt;code&gt;getAllCountries&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhmeere7hce5m4qb25eo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhmeere7hce5m4qb25eo.png" alt="function getAllCountries" width="800" height="660"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The URL can be changed based on the env (if needed)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijki84l2g3gyoagtaom8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijki84l2g3gyoagtaom8.png" alt="Env based URLs" width="800" height="449"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The response of the function would be something like this.&lt;/p&gt;

&lt;p&gt;An array of Country Data with their respective Flag image URL.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0zwzzcirja8ecksqjiq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz0zwzzcirja8ecksqjiq.png" alt="response of the API" width="800" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let us look at another serverless function that takes in a request parameter and gives the response of the specific country data, e.g. country name.&lt;/p&gt;

&lt;p&gt;Create another file in &lt;code&gt;pages/api&lt;/code&gt; as &lt;code&gt;getFlagUrlByCountryName.js&lt;/code&gt; and the serverless function would look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5grzm484282905j1qbhw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5grzm484282905j1qbhw.png" alt="Serverless function of getFlagUrlByCountryName" width="800" height="292"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And we can make the API call something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwa95wspod647nh42o0pq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwa95wspod647nh42o0pq.png" alt="API Call for getFlagUrlByCountryName" width="800" height="626"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The response would be something like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbwkhm007bvleqknck2p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmbwkhm007bvleqknck2p.png" alt="Response for getFlagUrlByCountryName" width="800" height="173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Going forward we can create serverless functions for other functions available in the &lt;a href="https://github.com/ronatskiy/country-flags-svg#api" rel="noopener noreferrer"&gt;API section&lt;/a&gt; of the country-flags library.&lt;/p&gt;

&lt;p&gt;You can find the same &lt;a href="https://github.com/timelessco/country-flags" rel="noopener noreferrer"&gt;here&lt;/a&gt;, you can also duplicate and deploy it in your Vercel account if you want to use it. :)&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it look to use the deployed version:
&lt;/h2&gt;

&lt;p&gt;Let us test in &lt;strong&gt;&lt;em&gt;Postman&lt;/em&gt;&lt;/strong&gt;. I have deployed my serverless function &lt;a href="https://country-flags-topaz.vercel.app/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;By Nationality&lt;/strong&gt;- &lt;a href="https://country-flags-topaz.vercel.app/api/getCountryByNationality" rel="noopener noreferrer"&gt;https://country-flags-topaz.vercel.app/api/getCountryByNationality&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffrocqsyrc76jfxwqvc5g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffrocqsyrc76jfxwqvc5g.png" alt="Postman image of getCountryByNationality API" width="800" height="405"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;2. &lt;strong&gt;By Name&lt;/strong&gt;- &lt;a href="https://country-flags-topaz.vercel.app/api/getFlagUrlByCountryName" rel="noopener noreferrer"&gt;https://country-flags-topaz.vercel.app/api/getFlagUrlByCountryName&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrph2touji1nfhac1fgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrph2touji1nfhac1fgx.png" alt="Postman image of getFlagUrlByCountryName API" width="800" height="417"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3. &lt;strong&gt;By ISO2 Code-&lt;/strong&gt; &lt;a href="https://country-flags-topaz.vercel.app/api/getFlagUrlByISO2Code" rel="noopener noreferrer"&gt;https://country-flags-topaz.vercel.app/api/getFlagUrlByISO2Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5qg40hytby039b4ys8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5qg40hytby039b4ys8c.png" alt="Postman image of getFlagUrlByISO2Code API" width="800" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4. &lt;strong&gt;By ISO3 Code-&lt;/strong&gt; &lt;a href="https://country-flags-topaz.vercel.app/api/getFlagUrlByISO3Code" rel="noopener noreferrer"&gt;https://country-flags-topaz.vercel.app/api/getFlagUrlByISO3Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favnfvhg1l00bp85zcoww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favnfvhg1l00bp85zcoww.png" alt="Postman image of getFlagUrlByISO3Code API" width="800" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;5. &lt;strong&gt;All Countries-&lt;/strong&gt; &lt;a href="https://country-flags-topaz.vercel.app/api/getAllCountries" rel="noopener noreferrer"&gt;https://country-flags-topaz.vercel.app/api/getAllCountries&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwt2jk8mq4k9lt0kjo3oa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwt2jk8mq4k9lt0kjo3oa.png" alt="Postman image of getAllCountries API" width="800" height="614"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This is&lt;/em&gt; &lt;a href="https://twitter.com/_iam_karthik" rel="noopener noreferrer"&gt;&lt;strong&gt;&lt;em&gt;Karthik&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; &lt;em&gt;from&lt;/em&gt; &lt;a href="http://timeless.co/" rel="noopener noreferrer"&gt;&lt;em&gt;Timeless&lt;/em&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you find this blog post helpful, do tell me in the comments would love to know your views. Thank you :)&lt;/p&gt;

&lt;p&gt;If you find any difficulties or see anything which could be done better, please feel free to add your comments!&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>nextjs</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
