Setting value in session variable so it can be used in other API scripts

I want to store the inserted recorded id in a session variable so I can check it in other API scripts but I just can’t seem to get it to work.

I am using the Set Session action to set the value.

Screenshot 2022-11-27 at 17.47.43

I can then output that value from within the same script. But when I try to access that value in another script, it’s not been updated.

I’ve used the Set Value action to output the session value for testing.

What am I doing wrong?

2 Likes

Have you defined the session variables correctly beforehand?

Do you mean like this?

Screenshot 2022-11-27 at 21.57.37

I’m kind of getting somewhere but need some further insight.

I can set the session variable if I run the API script directly (using the Open in Browser button) but it’s not set if the API script is called by a page. I need to make sure the session variable can’t be changed by the visitor so am trying to set it in the API script and then call it later from another API script.

I’ve tried using the Session Storage Manager on the page but that sets it client-side so I can manipulate the value using the dev console. It also seems to be different to the session variable set in the API even if they have the same name. I’m pretty sure I’d read that’s the case some time ago.

So, what am I missing in order to set the variable server-side in the API script when called by a page?

Sorry if this is all obvious to you, but just in case…

Session variables set inside a server api are different from those set using the session manager on front end pages. They are completely distinct items and do not cross paths ever.

Setting a session variables on the server is for that singular user session only. In other words, if you set a session variable in one browser window, and then run another script in another window, those are different user sessions. Sessions ARE shared across tabs.

If there is no identity from your insert (as in it did not happen) then there will be no session variable, as it would be null.

Establishing a session variable under Inputs, does not create any variable, it simply makes it selectable for use within the api. The set session is the only time a variable is actually created.

2 Likes

Yes. Ok. Good. So now you can use ‘set value’ variables with output on and pass them to the client side as required

1 Like

Basically the confusion comes from the fact that server sessions set by api actions and browser sessions set in the browser are not the same thing. To retrieve your server session value for use in the browser you must call an api action and output it via a set value stage

1 Like

Thank you all for your replies. They all confirm what I already knew but, sadly, didn’t explain why it wasn’t working for me!

I tried using the Session Storage Manager just to satisfy my conviction that server and browser sessions are different.

However, I have found what the issue was and it was my own misguided attempt to test it before actually doing it and it’s flagged up something which is worth sharing.

Scenario: You have an API script which sets a session variable. That var can be accessed by any other API script BUT NOT if you run that API directly unless the session was created by running it directly.

I was foolishly trying the confirm the value in the session var by creating a simple API script and running it directly but it wasn’t updating. Had I not tried to test it this way, I would have just continued to build the API script and it would have worked perfectly. As it now does!

So it’s not just a case of client-side sessions and server-side session being completely different, it’s also a case of how you run the API script.

Thanks everyone. Some really helpful responses. I was typing a different reply explaining what I was trying to do when the penny suddenly dropped so I built my functionality and viola!

I often find it useful to monitor the contents of session variables (server and local) for debugging etc. In relation to this, this thread might be helpful.

1 Like