is it good practice to call an action from an api?
is it good practice to call an action from an api? If so, should it be done?
1 Reply
Hi @cmilazzo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
In Wasp, Actions and APIs are designed to serve different purposes. Actions are primarily used for modifying the server's state and are typically called directly from the client side. They are not designed to be reactive and can be used without a hook, although Wasp does provide a
useAction
React hook for adding extra behavior to the Action.
On the other hand, APIs (or HTTP endpoints) are used when you want to expose some server-side functionality to the outside world, not just to the Wasp frontend.
If you want to call your Action from outside the Wasp app's frontend, as a regular HTTP endpoint, you'll have to use the API. It is possible to use Actions from outside of the app, but that's not what they're meant for and there's no stability guarantee.
You can avoid repeating the same logic twice by creating a separate function for the business logic and calling it from both the API and the Action. Here is an example:
So, while it's technically possible to call an Action from an API, it's not typically the recommended approach in Wasp. Instead, consider extracting the shared logic into a separate function that can be called from both the Action and the API.
Sources:
- Differences Between Queries and Actions (https://wasp-lang.dev/docs/data-model/operations/actions#differences-between-queries-and-actions)
- Discord support thread: How can I get current theme colors and set them to div ? > Answers Part 1 (https://discord.com/channels/686873244791210014/1222813122025226350)
- Discord support thread: How can I get current theme colors and set them to div ? > Answers Part 2 (https://discord.com/channels/686873244791210014/1222813122025226350)
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Sorry @cmilazzo, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist:
Perhaps try again in 15mins?