Tutorial API implementation doesn't work for me.

my main.wasp is
api upload_video { // APIs and their implementations don't need to (but can) have the same name.
fn: import { fooBar } from "@src/api/api",
httpRoute: (GET, "/api"),
auth: false,
middlewareConfigFn: import { apiMiddleware } from "@src/api/api"
}
api upload_video { // APIs and their implementations don't need to (but can) have the same name.
fn: import { fooBar } from "@src/api/api",
httpRoute: (GET, "/api"),
auth: false,
middlewareConfigFn: import { apiMiddleware } from "@src/api/api"
}
my src/api/api.jsx is
export const fooBar = (req, res, context) => {
console.log("req");
res.set("Access-Control-Allow-Origin", "*"); // Example of modifying headers to override Wasp default CORS middleware.
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` });
};

export const apiMiddleware = (config) => {
return config;
};
export const fooBar = (req, res, context) => {
console.log("req");
res.set("Access-Control-Allow-Origin", "*"); // Example of modifying headers to override Wasp default CORS middleware.
res.json({ msg: `Hello, ${context.user ? "registered user" : "stranger"}!` });
};

export const apiMiddleware = (config) => {
return config;
};
code compiles with no problems, but localhost:3000/api endpoint returns 401 instead of the api response. any ideas?
22 Replies
kapa.ai
kapa.ai4w ago
Hi @devrim. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- The issue you're experiencing might be related to the authentication setting in your API declaration. In your main.wasp file, you have set auth: false, which means that the API does not attempt to parse the JWT in the Authorization Header. However, if the request to the API includes an Authorization Header, and the JWT is invalid or expired, the server might still return a 401 Unauthorized error. If you're sure that the request does not include an Authorization Header, or that the JWT is valid, then the issue might be elsewhere. It could be related to the middleware configuration, or it could be a bug in Wasp. There is an open issue on the Wasp GitHub repository about providing more information during development when an error happens on the server, which might be related to your problem (source (https://github.com/wasp-lang/wasp/issues/91)). As a first step, I would suggest to check the request you're sending to the API, and make sure it does not include an Authorization Header, or that the JWT is valid. If that doesn't solve the problem, you might want to look into customizing the middleware configuration, as described in the Wasp documentation (https://wasp-lang.dev/docs/advanced/middleware-config). If none of these suggestions help, I would recommend to open a new issue on the Wasp GitHub repository (https://github.com/wasp-lang/wasp/issues), providing as much information as possible about your problem. The Wasp team is usually very responsive and will be able to help you troubleshoot the issue. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @devrim, 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?
devrim
devrim4w ago
just hitting the url with localhost:3000/api with no JWT - github issue is not helpful. ok cursor AI helped, if i do localhost:3001/api then it works..
MEE6
MEE64w ago
Wohooo @devrim, you just became a Waspeteer level 1!
devrim
devrim4w ago
but this doesn't solve how i access that endpoint when the app is deployed...
Gwaggli
Gwaggli4w ago
Depends where you deploy your backend. The API is part of your backend server. So you need to use the url of your backend server. If you used the standard wasp config and deployed it to fly.io your backend server address will look probably something like this: https://<yourAppname>-server.fly.dev So for example: https://devrim-server.fly.dev/api
devrim
devrim4w ago
hi that makes sense, but i'm getting a forever spinning wheel when i hit the deployed URL
No description
devrim
devrim4w ago
@Gwaggli if you can take a look.
devrim
devrim4w ago
i think something else is up, with google auth.. debugging
devrim
devrim4w ago
ok i'm sorry this is nothing to do with the api endpoint, it was complaining about the google oauth token, and not running the server at all. if you guys know what it's complaining about google token i'd appreciate that (followed the tutorial there fully as well) but if not please disregard the api not working request. thank you!
Gwaggli
Gwaggli4w ago
I am not using google oAuth so not sure, but seems like you are missing some env variables? Does it work locally, right? Did you set the env variables in fly.io as well? Personal/Videoupdate-server/Secrets? (https://fly.io/apps/videoupdate-server/secrets)
miho
miho4w ago
Which template are you using? How does your Wasp file look like, are you using Google auth? Did you set the env variables on Fly?
fustuk.
fustuk.4w ago
refused connection. is your app listening on 0.0.0.0:8080? make sure it is not only listening on 127.0.0.1
refused connection. is your app listening on 0.0.0.0:8080? make sure it is not only listening on 127.0.0.1
Maybe this is your issue, is your redirect url on localhost rather than pointing to the hosted domain You also have these errors, indicating your env doesnt have the values for google auth have you added that yet? (keep in mind .env is in .gitignore you have to add it on the server)
throw new Error(`${envVarName} env variable is required when using the ${provider.displayName} auth provider.`);

Error: GOOGLE_CLIENT_ID env variable is required when using the Google auth provider.
throw new Error(`${envVarName} env variable is required when using the ${provider.displayName} auth provider.`);

Error: GOOGLE_CLIENT_ID env variable is required when using the Google auth provider.
devrim
devrim4w ago
all problems here have been resolved with your help. thank you very much!
prof_mark
prof_mark3w ago
im getting this error and cannot figure out what is going on. i have the GOOGLE_CLIENT_ID in my .env.server file, but somehow I guess fly is not picking it up on my redeploys ok it turns out my issue was fixed by setting the env vars on fly manually as described here: https://wasp-lang.dev/docs/advanced/deployment/cli#environment-variables-1 my question is then why I am also setting them in .env.server and/or why these are not pull from the .env.server file and set for my automatically as part of deployment. I think this is also related to the issue here: https://github.com/wasp-lang/wasp/issues/2053
fustuk.
fustuk.3w ago
If I understand properly, you are asking why your env files are not pushed ? They are in .gitignore/.dockerignore so it doesnt leak info/keys.You should always set them manually when you deploy somewhere regardless of where you deploy and what frameworks you use
Want results from more Discord servers?
Add your server