PricingBlog

API calls within Actions.

  • the__starlord-1532643603946672170

    The_StarLord

    1 day ago

    I am making an API call within an Action, but need to attach an HTTP only cookie.

    I tried passing in the cookie through a input passing in {{ cookies.acces_token }} and have tried through javascript selection as well, but the DOM seems to be blocking it or something. Either way it seems that my header in the API call is a string literal "Bearer {{ cookies.access token }}" instead of the access token.

    I have 2 ideas on how to solve it, the first is asking here as it could be a backend Nordcraft setup that I just dont know.

    The second is a callback from the action so when a button is pressed it triggers a workflow or action outside of the action i am in... is this possible? If yes, where can I learn about it or can someone explain how I can access something outside of the action?
  • keddedev-1532653090245836950

    Kedde

    1 day ago

    The http only cookie can only be read server side so you would need to make sure you proxy the request (in the advanced settings of the API). That will use the Nordcraft proxy that finds the {{ cookies.access_token }} and replaces it with the actual cookie value.

    If you need to pass the http only cookie in the body of the request you also want to check the "Inject cookies in body" in the Advanced settings of the API.
  • the__starlord-1532699369969549342

    The_StarLord

    1 day ago

    Is the nordcraft proxy possible within an Action?
  • the__starlord-1532699789580566669

    The_StarLord

    1 day ago

    The actual call is happening within the Action through javascript, not through Nordcraft API.

    I am also not clear on if I can access other workflows, formulas, actions, etc. from within a page action. Is that possible through the ctx variable?
  • keddedev-1532739368689733683

    Kedde

    1 day ago

    You cannot access formulas and actions from within your javascript within a custom action. To use the Nordcraft proxy I would suggest still creating an API. If you need to pass information into the API from your custom js you could make use of Events (https://docs.nordcraft.com/actions/working-with-actions#set-up-events). That way when you call your action you can setup a workflow once the action triggers that event and in that workflow you would call the API using the nordcraft trigger rather than javascript. If you need custom code to run after the api call you can use the events "On success", "On error" or "On message" events in the api panel to trigger you custom action.
  • the__starlord-1532743839733911563

    The_StarLord

    1 day ago

    Thanks for the clarification! I understand generally how to use actions. In this particular case I have a complex piece of javascript generating visuals including a button. However when that button is pressed I need to send data back to my database. To make the call securely I need to attach the access token cookie into the Bearer auth. When using my admin secret without that cookie, the call completed just fine, but making it securely has now broken the complex action element. I know actions are probably not to be used to create visuals, however making something like an operational calendar is just too complex a piece to quickly get done for the prototype with a client.

    So, I am wondering how to get this button to either make a callback to the API or trigger the workflow so I can possibly accomplish this without having to recreate it all.
  • the__starlord-1532747476044611585

    The_StarLord

    1 day ago

    Example of what I mean. This is not the component I am working on, but it is effectively the same. This entire calendar view is generated by Javascript. Obviously not the intention of an Action ( I assume).

    When I click the button, I am able to make calls to my backend, and "reserve" my spot. However, that is using my admin-secret totally exposed. So doing this the right way using Bearer Auth, works outside of the action ( I have that working across my app), it just breaks on this button. I tried passing in the access_token cookie as an input, but as we discussed... no luck.

    I would like to avoid having to create an entire booking calendar component within the editor and then map stuff to the Calendar view, especially in the age of LLMs that can assist with that to speed it up.

    If there is a way to get that access_token attached within the call that works with the admin secret, that would be awesome. However, if the button can trigger a workflow instead that uses the API call, I am fine with that too... I am just not seeing in the documentation how to trigger a workflow from an action (and maybe that is just not possible).
    1532747475851546654-image.png
  • keddedev-1532754308100128919

    Kedde

    1 day ago

    I havent actually tried manipulating DOM from the custom action like that but if you create your button in there and attach a click event to that maybe you would be able to trigger a Nordcraft event from within that click event? So if you add an event in the left panel to the action called buttonClick something like this might work?
    buttonRef.addEventListener("click", function() {
    ctx.triggerActionEvent("buttonClick", data)
    });


    Then whereever you trigger the actual custom event you would see the "buttonClick" callback you can open up and trigger your API
  • the__starlord-1532758819434922124

    The_StarLord

    1 day ago

    I will try it! If it works, worth documenting in the advanced section 😄
  • the__starlord-1532761670416334888

    The_StarLord

    1 day ago

    Consensus... it works!
  • 1532761911492350044-image.png
  • @Kedde THANK YOU! Saving me here!!
  • Tod-1532761985970868335

    Tod

    1 day ago

    Great job @The_StarLord! Your contribution to the Nordcraft Community just made you advance to Community Level 4! 🌲
  • keddedev-1532763021351452898

    Kedde

    1 day ago

    🥳🥳🥳