Invoke Netlify Functions locally

Prince Wilson
InstructorPrince Wilson
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

In this lesson, you will learn how to put your Netlify Functions in action without having to deploy them on the Internet. We will make use of the Netlify CLI to run our application and invoke the Netlify Functions directly on your computer.

Prince Wilson: [0:00] To get started, we're going to build a netlify.toml file. Inside of here, we're going to declare, during the build phase, their functions are located in the functions directory.

[0:12] Currently, we don't have that existing, so let's make sure we're going to add that. We want this at the top most level. Let's add a folder called functions. Inside of that folder, we're going to declare a file called hello.js.

[0:25] In this file, we must have exports.handler equal to async function with two parameters -- event and context. We won't be using this in this video, but it's important to know that they are there.

[0:36] This function needs to return a json object with a key of statusCode declaring, "Hey, everything is OK," so 200. The key body, the body of the response, so in our case, "Hello, World." Congratulations, you made a Netlify function.

[0:52] Go into your terminal. Write the command, netlify dev. This will run the application as well as get everything set up for Netlify. In another terminal session, make sure to run the command, netlify functions:invoke. This will invoke our function locally. Inside of the terminal, you should see the output, "Hello, World."

[1:15] To recap, we started by creating a netlify.toml file where we defined where Netlify should build a function. We went ahead and build our first function inside up here returning the body, "Hello, World."

[1:27] We went inside of our terminal and wrote the command, netlify dev. We see the application has started our local dev server. We opened another terminal session, wrote netlify functions:invoke and saw the output of our function, "Hello, World."

egghead
egghead
~ just now

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today