custom http api endpoint

Hello. I am following some of the documents for custom http api endpoint. 8 gave an 'api foobar' defined in the main.wasp but when I try to import foobar from 'wasp/server/api' in the api ts file it says 'no export member 'foobar'' . Am I missing something?
8 Replies
kapa.ai
kapa.ai2mo ago
Hi @TheHumanMachine. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- It seems like you might be having an issue with the way you're importing the foobar function in your TypeScript file. In the Wasp framework, the api declaration in the main.wasp file is used to tie a JavaScript function to a certain endpoint. The function itself should be defined in a separate file and then imported into the main.wasp file. Here's an example of how you might declare and import the function: In your main.wasp file:
api fooBar {
fn: import { fooBar } from "@src/apis",
httpRoute: (GET, "/foo/bar")
}
api fooBar {
fn: import { fooBar } from "@src/apis",
httpRoute: (GET, "/foo/bar")
}
And in your src/apis.ts file:
import { FooBar } from "wasp/server/api";

export const fooBar: FooBar = (req, res, context) => {
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` });
};
import { FooBar } from "wasp/server/api";

export const fooBar: FooBar = (req, res, context) => {
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` });
};
In this example, fooBar is a function that is defined in the src/apis.ts file and then imported into the main.wasp file. The FooBar type is generated by Wasp based on the api declaration in the main.wasp file. If you're still having trouble, it might be helpful to double-check that the fooBar function is being correctly exported from your src/apis.ts file and that the file is located in the correct directory. If the function is not being exported correctly or the file is not in the correct location, you might see the "no export member 'foobar'" error message. For more information, you can refer to the Wasp documentation on defining the API's NodeJS implementation (https://wasp-lang.dev/docs/advanced/apis#defining-the-apis-nodejs-implementation). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @TheHumanMachine, 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?
TheHumanMachine
TheHumanMachine2mo ago
The code provided by the bot is pretty much what I have. The foobar isnt available for import from the 'wasp/server/api'
martinsos
martinsos2mo ago
Hey @TheHumanMachine , we can help faster and easier if you give us as much info as you can. Let's try to exactly nail down what you have where. 1. Post code of your api foobar declaration in main.wasp. 2. Post code of how you implement that in JS/TS. We don't need the whole code, but would be good to see something like: "I have export const fooBar ... in src/apis.ts. Just to be clear, the error you get about export member foobar not existing -> that error is being caused by failed attempt to import that foobar in api declaration in main.wasp file? Or are you trying to import it somewhere else and getting this error?
TheHumanMachine
TheHumanMachine2mo ago
Hey I solved the issue I was having. It seemed all I needed to do was clean the project. @martinsos I do have another issue though. the code I have added to the open saas main.wasp
api foobar{
fn: import {fooBar} from "@src/server/apis",
httpRoute: {GET, "/foo-bar")
}
api foobar{
fn: import {fooBar} from "@src/server/apis",
httpRoute: {GET, "/foo-bar")
}
apis.ts
import { FooBar } from "wasp/server/api";

export const fooBat : FooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", "");
res.json({msg:`Hello`});
};
import { FooBar } from "wasp/server/api";

export const fooBat : FooBar = (req, res, context) => {
res.set("Access-Control-Allow-Origin", "");
res.json({msg:`Hello`});
};
When I go to this endpoint I dont get anything. Everything compiled.
miho
miho2mo ago
You are exporting fooBat and using fooBar in the Wasp file?
TheHumanMachine
TheHumanMachine2mo ago
It's a typo I don't have discord on my dev laptop. It is foobar. It's literally the same code as in the example with a minor change to the return json I also tried to see if the mage ai generator thing would help. It doesnt understand api or http endpoint. Which is interesting...
martinsos
martinsos3w ago
Hi @TheHumanMachine , sorry for dropping the ball on this! Ah yes, we haven't thought Mage about the api. As for your issue: Hm that is quite peculiar, that you don't get anything. How do you go to that route? Though browser? Whta do you get - blank screen? What if you visit that route via curl?
TheHumanMachine
Eh I moved on to fastapi.