# Serverless Function API Calls

**URL:** <https://community.hubspot.com/t/serverless-function-api-calls/49266>\
**Category:** APIs & Integrations\
**Tags:** cms-content, developer-tools\
**Created:** [January 5, 2022, 5:26pm UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266 "2022-01-05T17:26:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLouis85](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@JLouis85](https://community.hubspot.com/u/JLouis85)\
**Post date:** [January 5, 2022, 5:26pm UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/1 "2022-01-05T17:26:34Z")

</div>

Is it possible to pass parameters and data back and forth between the FE and a serverless function?

We’re attempting to call a private API in the serverless function, and we were hoping we could set up an endpoint using serverless-express, but it looks like we probably need to use webpack for any packages outside a small handful of supported packages.

Wanted to confirm it’s even possible to use serverless-express with webpack in this way before attempting it.

Also, open to other ways of accomplishing the same thing. The main goal is to pass parameters to the serverless function and get json data back.

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [January 5, 2022, 5:59pm UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/2 "2022-01-05T17:59:24Z")

</div>

Hi @JLouis85 ,

I’m pretty sure you do not need serverless-express to expose an endpoint.

In your ‘[serverless.json](https://developers.hubspot.com/docs/cms/data/serverless-functions#serverless-json)’ you can define the endpoints you want to expose with your serverless function. This could look something like this:

```lia-code-sample
{
 "runtime": "nodejs12.x",
 "version": "1.0",
 "secrets": ["APIKEY"],
 "endpoints": {
 "randomgetmethod": {
 "method": "GET",
 "file": "randomgetmethod.js"
 },
 "randompostmethod": {
 "method": "POST",
 "file": "randompostmethod.js"
 }
 }
}

```

If you send a POST request to https://{domainName}/\_hcms/api/randompostmethod you can pass it [params or a body](https://developers.hubspot.com/docs/cms/data/serverless-functions/reference#function-file).

Let’s say I would send a POST request tohttps://{domainName}/\_hcms/api/randompostmethod?param=test&secondparam=anothertest with some params, you can access those in the function like this:

```lia-code-sample
exports.main = (context, sendResponse) => {
 const param = context.params.param[0]
 const secondParam = context.params.secondparam[0]

 // sendResponse is a callback function you call to send your response.
 sendResponse({ body: { message: `first param is ${param} and second param is ${secondParam}` }, statusCode: 200 });
};

```

You could Axios in your serverless function to make calls to your private API.

---

<div class="post-metadata">

**Author:** ![JLouis85](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@JLouis85](https://community.hubspot.com/u/JLouis85)\
**Post date:** [January 5, 2022, 8:54pm UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/3 "2022-01-05T20:54:52Z")

</div>

Thank you! We’ll test out your suggestion, and let you know if we have any more questions.

---

<div class="post-metadata">

**Author:** ![bsha100](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@bsha100](https://community.hubspot.com/u/bsha100)\
**Post date:** [March 16, 2022, 1:28am UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/4 "2022-03-16T01:28:04Z")

</div>

I am passing a variable ‘email’ in a query string parameter ( [ExampleURL](http://exampleurl.com?email=bob133@gmail.com&portalid=12345678)).

Within my functions file I am able to get portalid using context.params.portalid[0], as a test. Email, however does not return a value when using context.params.email[0]. Any suggestions?

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [March 16, 2022, 7:22am UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/5 "2022-03-16T07:22:58Z")

</div>

Hi @bsha100 ,

Could you try the following URL instead? I had some issues in the past with getting the first param as well.

> **[ExampleURL](https://exampleurl.com/?random=test&email=bob133@gmail.com&portalid=12345678)**

---

<div class="post-metadata">

**Author:** ![bsha100](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@bsha100](https://community.hubspot.com/u/bsha100)\
**Post date:** [March 16, 2022, 7:54pm UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/6 "2022-03-16T19:54:46Z")

</div>

Thanks for the quick response @Teun, I have update the code to add ?random=test at the beginning of the query string varibles, however when I set console.log(context.params.email) in the endpoint, it is not updating. I added console.log(“TEST”), as well, and am not getting any updates with calls to **hs logs** in the CLI. Is there some sort of caching mechanism? Calling the endpoint directly shows the update, so I’m wondering if the call from the HTML file is breaking. Here is the code. I have replaced the portal ID with x’s.  
**test\_form.html**

\<!--

templateType: page

isAvailableForNewContent: true

label: Submission Test Form

--\>

\<!doctype html\>

```
\<html>

    \<head>

        \<meta charset="utf8">

        \<title>{{ content.html\_title }} Page\</title>

        \<meta name="description" content="{{content.meta_description}}">

            {{standard\_header\_includes}}

        \<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js">\</script>

        \<script>

                $(window).on('load', function(){

                    email = '';

                    apicall = '';

                    setTimeout(function () {

                        $("input\[name='email']").blur(function(){

                            email = $("input\[name='email']").val();

                          // alert(email);

                        });

                    }, 1000);

                });

                bodyFormData = new FormData();

                var requestOptions = {

                'method': 'GET',

                'headers': {

                'Content-Type': 'application/json',

                },

                };

                fetch("[http://xxxxxxxx.hs-sites.com/\_hcms/api/test?random=n&email=](http://xxxxxxxx.hs-sites.com/_hcms/api/test?random=n&email=)"+email+"&portalid=xxxxxxxx", requestOptions)

            .then(response => response.text())

            .then(result => console.log(result))

            .catch(error => console.log('error', error));

        \</script>

    \</head>

    \<body>

        \<h1>EMAIL TEST\</h1>

        {% module "page\_template\_logo" path="@hubspot/logo" label="Logo" %}

        {% module "page\_template\_rich\_text" path="@hubspot/rich\_text" label="Rich\_text" %}

        {% module "page\_template\_form" path="@hubspot/form" label="Form" %}

        \<div>

        \</div>

        {{standard\_footer\_includes}}

    \</body>

\</html>

```

**serverless.json**

{

“runtime”: “nodejs12.x”,

“version”: “1.0”,

“environment”: {},

“endpoints”: {

```
"test": {

   "file": "test.js",

   "method": "GET"

}

```

},

“secrets”: [“hapikey”]

}

---

<div class="post-metadata">

**Author:** ![bsha100](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@bsha100](https://community.hubspot.com/u/bsha100)\
**Post date:** [March 16, 2022, 8:41pm UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/7 "2022-03-16T20:41:53Z")

</div>

@Teun, inexplicably, the form is submitting correctly again, however I am still not getting a value for ‘email’. The url I am using as the endpoint is structured like this currently.  
**[http://xxxxxxxx.hs-sites.com/\_hcms/api/test?random=n&email=](http://xxxxxxxx.hs-sites.com/_hcms/api/test?random=n&email=)[bob@gmail.com](mailto:bob@gmail.com)&portalid=xxxxxxxx**  
I am random is coming out as ‘ **n** ’ when using **console.log(context.params.random[0])** , however email is still coming out blank.

---

<div class="post-metadata">

**Author:** ![bsha100](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@bsha100](https://community.hubspot.com/u/bsha100)\
**Post date:** [March 16, 2022, 10:17pm UTC](https://community.hubspot.com/t/serverless-function-api-calls/49266/8 "2022-03-16T22:17:12Z")

</div>

@Teun, I figured it out. I was defining the variable inside a function. Putting the fetch() call within the function solved the problem. Thanks for your input and the solution, which did work once I realized my mistake.
