Query String from API

Quick question and hopefully a simple one.

I have an API call that returns a list of contacts in a particular list.

vidoffset = data.response[‘vid-offset’];

https://mydomain.com/_hcms/api/getcontacts?voff='+vidoffset

But how do I get the value of voff (vid_offset from the returned json data) from within my function…

var vidOffset = voff;

https://api.hubapi.com/contacts/v1/lists/‘+listID+’/contacts/all?count=‘+numContacts+vidOffset+’&property=associatedcompanyid&property=firstname&property=lastname’

… so i can use it in subsequent calls?

Thanks in advance!

C

Hey @Chrouglas,

In the response body of that API request, you should find a “has-more” and a “vid-offset” attribute at the end of it as seen here.

Hope that helps!

Thanks for the quick response. I dont think I was clear in my question :slightly_smiling_face:

I got that part and im grabbing it with

vidoffset = data.response[‘vid-offset’];

My question is if I make a second call with that vid-offset as a query string param, how do I use access that value inside the serverless function.

I.e.

const contacts = ‘https://api.hubapi.com/contacts/v1/lists/1/contacts/all?count=50&property=associatedcompanyid&property=firstname&property=lastname&vidOffset=HOW-DO-I-GET-THIS-FROM-THE-PREVIOUS-API-CALL’;

Oh I see! Your question is more syntax than HubSpot. I missed the line where you mentioned your vidoffset variable.

Apologies in advance if I am still not understanding your question.

Are you trying to perform a loop? You could store the vidOffset in a separate variable outside the loop then update it on every response.

let vidOffset = 0;
while(vidOffset !== 0) {
 // perform API request
 // on response, update the vidOffset value
 // if the response' has-more is false, set the vidOffset to 0 to break the loop
}

If you are asking how to simply add the value of your response in the string, there are a couple ways:

Using `:

/contacts/v1/lists/1/contacts/all?count=50&property=associatedcompanyid&property=firstname&property=lastname&vidOffset=${vidoffset}

Normal quotes:

"/contacts/v1/lists/1/contacts/all?count=50&property=associatedcompanyid&property=firstname&property=lastname&vidOffset=" + vidoffset

I would also double check your variable spelling. In your post you are storing it as “vidoffset” but in your URI that you posted it is “vidOffset”.

Hope that helps!

Thanks for the suggestions. Ive gotten it working and it was indeed just a syntax thing. I was literally just looking for how to access a query string parameter from inside the function and while it was in fact staring me in the face…

… I was getting so many errors that I attributed to how i was calling for the variable.

Here is how it ended up…

context.params.paramname

Thanks again for your help. I’m sure I’ll be asking for it again some time :slightly_smiling_face:

Awesome! I’m happy you got it all sorted out :grin: