# API call - building payload

**URL:** <https://discourse.nodered.org/t/api-call-building-payload/68860>\
**Category:** General\
**Created:** [11 October 2022 11:41 UTC](https://discourse.nodered.org/t/api-call-building-payload/68860 "2022-10-11T11:41:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stain3565](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stain3565/32/68179_2.png) [@stain3565](https://discourse.nodered.org/u/stain3565)\
**Post date:** [11 October 2022 11:41 UTC](https://discourse.nodered.org/t/api-call-building-payload/68860/1 "2022-10-11T11:41:27Z")

</div>

Still learning and struggling with some basics.

I am building a call to an api in a function node. I have a hardcoded version of the call, within the code, so I can prove a call works and test my building of the data quickly (comment either in/out)

```auto
var todayd = new Date();
var yesterdayd = new Date();
yesterdayd.setDate(yesterdayd.getDate() - 1);
todayd = (todayd.getFullYear() + '-' + ('00' + (todayd.getMonth() + 1)).slice(-2) + '-' + ('00' + todayd.getDate()).slice(-2) + 'T00:00:00');
yesterdayd = (yesterdayd.getFullYear() + '-' + ('00' + (yesterdayd.getMonth() + 1)).slice(-2) + '-' + ('00' + yesterdayd.getDate()).slice(-2) + 'T00:00:00');

var message = 'from:"' + yesterdayd + '",';
message+= 'to:"' + todayd + '",';
message += ' period: "P1W",function: "sum"'
msg.payload = {
/* hardcoded works  
"from": "2022-10-10T00:00:00",
    "to": "2022-10-11T00:00:00",
    "period": "P1W",
    "function": "sum"
*/
/* does not work */   
message

    }
msg.headers = {};
msg.headers['accept'] = "application/json";
msg.headers['token'] = global.get("hildebrand_glowmrkt_token");
msg.headers['applicationId'] = global.get("hildebrand_glowmarkt_applicationId");
msg.url = "https://api.glowmarkt.com/api/v0-1/resource/" + global.get("hildebrand_glowmrkt_resourceIdAMRAPI") + "/readings?";
return msg;

```

Building the code is failing. The difference between the payloads is below:

Hardcoded (and works):

```auto
from: "2022-10-09T00:00:00"
to: "2022-10-11T00:00:00"
period: "P1W"
function: "sum"

```

My attempt at building it:

```auto
message: "from:"2022-10-10T00:00:00",to:"2022-10-11T00:00:00", period: "P1W",function: "sum""

```

I understand the reason why my version is invalid as it is clearly treating the payload as a "message" variable. However, I don't know how to code this so that it is simply the 4 variables in the object created.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [11 October 2022 11:53 UTC](https://discourse.nodered.org/t/api-call-building-payload/68860/2 "2022-10-11T11:53:54Z")

</div>

This is because you have output your code as a string and not as an object.

You need

```auto
msg.payload = {
   from: yesterdayd,
   to: todayd,
   period: "P1W",
   function: "sum",
}

```

---

<div class="post-metadata">

**Author:** ![stain3565](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stain3565/32/68179_2.png) [@stain3565](https://discourse.nodered.org/u/stain3565)\
**Post date:** [11 October 2022 11:58 UTC](https://discourse.nodered.org/t/api-call-building-payload/68860/3 "2022-10-11T11:58:15Z")

</div>

> [@TotallyInformation](#):
>
> ```auto
> from: yesterdayd,
> to: todayd,
> period: "P1W",
> function: "sum",
> 
> ```

Thanks so much.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [25 October 2022 11:58 UTC](https://discourse.nodered.org/t/api-call-building-payload/68860/4 "2022-10-25T11:58:49Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
