DEV Community

Cover image for Getting started with Serverless Functions using Vercel — I
Karthik_B for This is Learning

Posted on • Originally published at Medium

Getting started with Serverless Functions using Vercel — I

Hey all!

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

  1. Country Name
  2. Nationality
  3. iso2Code
  4. iso3Code

This library provides functions that return the required data.

You can check the Github repo here.

So I thought why can't I just wrap each functionality around a serverless function and deploy it in Vercel.

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.

Let's get started.

Creating a new Next.js App. The doc recommends using create-next-app which sets up everything for us.

npx create-next-app@latest
Enter fullscreen mode Exit fullscreen mode

After the installation is complete, run npm run dev to start the development server.

Our application would run in http://localhost:3000 , let us visit it to view our application.

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

index.js file of project

With that done, let's get into Serverless Functions.

So basically a serverless function is a chunk of code, a function, that takes a request and gets us a response.

It can be written in different backend languages.

I have chosen that language to be Javascript, the mother of all languages. 🔥

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

The basic structure of a Serverless Function is something like this:

structure of serverless function

Any query or params we send would be inside the request object. We will get to it soon.

Using the request we can perform any operation and return the response.

Let me take the simplest example, the Country Flags library has a function that returns all the countries and their data.

Create a new file in pages/api a directory called getAllCountriesData.js and add the below code.

import { countries } from "country-flags-svg";

export default function handler(_req_, _res_) { 

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

}
Enter fullscreen mode Exit fullscreen mode

Now in pages/index.js file, let us use the above function.

This is a simple network request. I am using Axios. Let us create an async functiongetAllCountries

function getAllCountries

The URL can be changed based on the env (if needed)

Env based URLs

The response of the function would be something like this.

An array of Country Data with their respective Flag image URL.

response of the API

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.

Create another file in pages/api as getFlagUrlByCountryName.js and the serverless function would look something like this:

Serverless function of getFlagUrlByCountryName

And we can make the API call something like this:

API Call for getFlagUrlByCountryName

The response would be something like this.

Response for getFlagUrlByCountryName

Going forward we can create serverless functions for other functions available in the API section of the country-flags library.

You can find the same here, you can also duplicate and deploy it in your Vercel account if you want to use it. :)

How does it look to use the deployed version:

Let us test in Postman. I have deployed my serverless function here.

  1. By Nationality- https://country-flags-topaz.vercel.app/api/getCountryByNationality

Postman image of getCountryByNationality API

2. By Name- https://country-flags-topaz.vercel.app/api/getFlagUrlByCountryName

Postman image of getFlagUrlByCountryName API

3. By ISO2 Code- https://country-flags-topaz.vercel.app/api/getFlagUrlByISO2Code

Postman image of getFlagUrlByISO2Code API

4. By ISO3 Code- https://country-flags-topaz.vercel.app/api/getFlagUrlByISO3Code

Postman image of getFlagUrlByISO3Code API

5. All Countries- https://country-flags-topaz.vercel.app/api/getAllCountries

Postman image of getAllCountries API


This is Karthik from Timeless.

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

If you find any difficulties or see anything which could be done better, please feel free to add your comments!

Top comments (0)