custom http api

can you call a custom api from the server side in an action?
5 Replies
kapa.ai
kapa.ai3d ago
Hi @Bruno Njeru. 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 a custom HTTP API endpoint from the server side in an action by making an HTTP request to that endpoint using standard Node.js libraries (like axios or fetch). Wasp's custom APIs are just regular HTTP endpoints, so you can consume them from anywhere—including your own server code—by issuing an HTTP request to the desired route. However, there is no special Wasp helper for calling custom APIs from server code; you need to use a standard HTTP client. The Wasp documentation and discussions confirm that custom APIs are accessible externally and from anywhere that can make HTTP requests, including your own server logic. For example, the docs state:
To use the API externally, you simply call the endpoint using the method and path you used. For example, if your app is running at <https://example.com> then from the above you could issue a GET to <https://example/com/foo/callback> (in your browser, Postman, curl, another web service, etc.)
Using the API externally This applies equally to server-side code: just use an HTTP client to call your custom API endpoint. There is no mention in the knowledge sources of a direct, internal function call or shortcut for invoking a custom API from server code—so you must use HTTP requests. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Bruno Njeru, 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?
Bruno Njeru
Bruno NjeruOP3d ago
its being called internally, so well use WASP WEB CLIENT URL then the api route @kapa.ai ?
kapa.ai
kapa.ai3d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- If you are calling a custom API endpoint from your server-side code (such as inside an action), you should use the URL where your server (the backend) is running, not the WASP_WEB_CLIENT_URL (which is for the frontend). The WASP_WEB_CLIENT_URL is intended for the server to know where the frontend is deployed, for things like CORS and OAuth redirects—not for internal server-to-server communication. For internal calls from the server to its own API endpoints, you should use the server's own URL (which you can set with the WASP_SERVER_URL environment variable). If you are running locally, this is typically http://localhost:3001 or whatever port your server is running on. In production, it would be the deployed backend URL. The knowledge sources clarify the purpose of these environment variables: - WASP_WEB_CLIENT_URL: "The URL where you plan to deploy your frontend app is running... The server needs to know about it to properly configure Same-Origin Policy (CORS) headers." - WASP_SERVER_URL: "The URL where the server is running... The server needs it to properly redirect users when logging in with OAuth providers like Google or GitHub."
Environment Variables So, for internal server-side HTTP calls to your own custom API endpoints, use the backend/server URL (WASP_SERVER_URL), not the frontend/client URL (WASP_WEB_CLIENT_URL). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Bruno Njeru, 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?
Bruno Njeru
Bruno NjeruOP3d ago
in the client we use import { api } from "wasp/client/api"; how do we call custom api from the server side in an action @kapa.ai
kapa.ai
kapa.ai3d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- To call a custom API from the server side in an action, you do not use the client helper import { api } from "wasp/client/api";—that is only for client-side code. On the server side (e.g., inside an action), you should use a standard HTTP client library such as axios or node-fetch to make an HTTP request to your custom API endpoint. There is no special Wasp server-side helper for this purpose mentioned in the knowledge sources. For example, if your custom API is available at /foo/bar, and your server is running locally on port 3001, you would do something like:
import axios from 'axios';

const response = await axios.get('<http://localhost:3001/foo/bar'>);
console.log(response.data);
import axios from 'axios';

const response = await axios.get('<http://localhost:3001/foo/bar'>);
console.log(response.data);
The knowledge sources confirm that custom APIs are just regular HTTP endpoints and can be called from anywhere that can make HTTP requests, including your own server code. There is no mention of a built-in Wasp server-side helper for calling custom APIs from actions or other server code, so you should use a standard HTTP client library for this purpose Docs: Custom HTTP API Endpoints. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @Bruno Njeru, 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?

Did you find this page helpful?