⚠️ This lesson is retired and might contain outdated information.

Serverless Function 2

Tom Chant
InstructorTom Chant
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 months ago

In this lesson, we start by cleaning up a serverless function for deployment on GitHub via VS Code. We remove unnecessary code, export the handler function, and bring in the OpenAI library using npm install. Since we don't have access to the API key stored in the env.js file, we retrieve it from the Netlify environment variable using process.env.

Next, we uncomment code in index.js and replace ConversationString with event.body in the serverless function to handle incoming fetch requests correctly. The lesson emphasizes the syntax difference between front-end and Node.js, explaining how the serverless function works separately from the front-end code. We end by discussing the need to handle the API response in the next scrim, preparing learners for the next part of the tutorial.

[00:00] I've just pasted this serverless function into the Scrimba editor, and again, when we're done, it will need to go back into VS Code so it can be pushed to GitHub to trigger a redeploy. Now, before we do anything else, let's delete all of the code we don't need. Okay, that is the bare bones of the serverless function. Now, what we've got at the end might

[00:20] look a little bit unfamiliar. Don't worry about that. That is just exporting this handler function to make it available to the rest of the app. And that syntax there is actually from Node.js, so it's not familiar to front-end developers. The next thing to do is bring in OpenAI. And

[00:38] remember, we used npm install to load in the dependencies we need so we have access to them across our project, including in this serverless function. Now, the code we need to do that is code we're very familiar with. In fact, we just commented it out in index.js. So let's head over

[00:55] to index.js and let's uncomment that. Now, we will not be importing our API key from this env.js file. Remember, we've ignored that or deleted it. It's not available on Netlify. So let's delete

[01:10] that line of code. Now, the API key that we've got stored in the Netlify environment variable is available to us in this serverless function, and we can access it using process.env and then the name of the API key. So luckily, we don't need to change this line of code at all because

[01:30] this is going to fetch our API key from the environment variable we set up earlier. So now we've bought in OpenAI, we need to actually use it down here inside the tri block. So let's head back to index.js and we'll take the second block of code that we've got commented out right here.

[01:49] That is looking pretty good, but we need to do something with ConversationString. We don't have access to ConversationString in this file, and we can't just import it as this serverless function will not be part of the front-end code. Remember, we're actually calling this serverless function

[02:06] using a fetch request. And we've got the fetch request right here, and ConversationString is being sent in the body of the fetch request. Now back in FetchAI, we're taking in an event parameter and from that event parameter, we can actually access the body. So I'm going to come down here

[02:25] and replace ConversationString with event.body. So now when the fetch request comes in, the ConversationString will be in the body. FetchAI takes in the event parameter and accesses

[02:39] ConversationString, which will be stored in the body of the event object. Okay, I think that's looking good. We can't test it yet because first we need to deal with what we actually get back from the API. We've got this return down here, so we need to do something with that. Let's come onto it

[02:57] in the next scrim.

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