DEV Community

Cover image for  Create Your Own Quote API with Netlify Functions - Part 1
Liz Lam
Liz Lam

Posted on • Edited on

1 1

Create Your Own Quote API with Netlify Functions - Part 1

Photo by Jon Tyson on Unsplash

This is a 3 part exercise on how to create and use your own quote API with Netlify functions.

A serverless function is a single purpose function that is maintained and hosted on a cloud provider's infrastructure. AWS Lambdas is Amazon's solution to serverless functions and you can think of Netlify functions as a friendly wrapper around AWS Lambdas.

Let's start by installing the Netlify command line interface:

npm install -g netlify-cli
Enter fullscreen mode Exit fullscreen mode

Using Create React App, set up a new project and navigate into it:

npx create-react-app my-random-quotes
cd my-random-quotes
Enter fullscreen mode Exit fullscreen mode

Create a netlify.toml file with the following content:

[build]
  functions = "functions/"
Enter fullscreen mode Exit fullscreen mode

This will define where we will put our API code. In this case, a functions directory relative to our project root.

Let's create a starter function:

netlify functions:create
Enter fullscreen mode Exit fullscreen mode

This will invoke an interactive prompt. Pick the js-hello-world template and name your function quotes.

A quotes subdirectory will be created under the functions directory. It will also create a quotes.js file.

Open the quotes.js file and replace the contents with the following:

// $project_root/functions/quotes/quotes.js
exports.handler = async (event, context) => {

  const quotes = [
    {"quote": "You gotta be kidding me?"},
    {"quote": "Will you be my neighbor?"},
    {"quote": "Liz is awesome!"}
  ]; 

  const number = Math.floor(Math.random() * quotes.length);
  const quote = quotes[number];

   return {
     statusCode: 200,
     body: JSON.stringify(quote)
   };

};
Enter fullscreen mode Exit fullscreen mode

Don't worry too much about the content of this code if it seems unfamiliar. It's just a bit of Node.js that will randomly return a quote from the quotes array.

Start a development environment with the following command:

netlify dev
Enter fullscreen mode Exit fullscreen mode

Ok, the hard part is over! Let's test our function locally by opening up a browser and navigating to localhost:8888/.netlify/functions/quotes.

If everything goes as expected, you should see a JSON object containing a random quote.

In the next post, we'll setup a Netlify account and deploy our function.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay