4 Replies
Hi @cmilazzo. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Yes, you can call an API from a Wasp action. Actions in Wasp run on the server side, which means they have full access to make HTTP requests to external APIs.
When implementing an action, you can use standard Node.js HTTP clients like
fetch
, axios
, or any other HTTP client library to make API calls to external services.
Here's a pattern you might follow when implementing an action that calls an external API:
This approach is useful when you need to integrate with third-party services or when you have an existing API that you want to use alongside your Wasp application. As mentioned in one of the Discord support threads, you can even use this pattern to integrate with Python applications by exposing them as APIs and then calling them from your Wasp actions Discord support thread.
Remember that when calling APIs from actions, you're doing so from the server side, which means you can safely include API keys and other sensitive information that shouldn't be exposed to the client.
----------------------
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?can this be done with an internal api without making use of http?
@kapa.ai can this be done with an internal api without making use of http?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Yes, you can call another action or query from within an action without making HTTP requests. Wasp allows you to directly import and call other operations from your server-side code.
There are two main approaches to calling one action from another:
1. Import from wasp/server/operations
You can import the action you want to call from
wasp/server/operations
:
As mentioned in the Discord support thread, the type support for this approach is limited, but it works functionally Discord support thread.
2. Extract shared logic into helper functions
Another approach is to extract the shared business logic into separate helper functions that can be called from multiple actions:
This pattern was suggested in a Discord thread as a way to avoid duplicating logic between API endpoints and actions Discord support thread.
By using these approaches, you can maintain a clean separation of concerns while reusing logic across different parts of your server-side code without making HTTP requests.
Remember that when calling one action from another, you need to properly handle the context object to maintain authentication and other contextual information.
Let me know if you need more specific examples for your use case!
----------------------
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?Thank you. That helped.