Tutorial API implementation doesn't work for me.
my main.wasp is
my src/api/api.jsx is
code compiles with no problems, but localhost:3000/api endpoint returns 401 instead of the api response. any ideas?
22 Replies
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?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..
Wohooo @devrim, you just became a Waspeteer level 1!
but this doesn't solve how i access that endpoint when the app is deployed...
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
hi that makes sense, but i'm getting a forever spinning wheel when i hit the deployed URL
@Gwaggli if you can take a look.
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!
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)
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?
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)
all problems here have been resolved with your help. thank you very much!
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
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
ofc these should not be checked in to version control, but shouldnt it be fine to send the .env files (or at least the values from there) to the fly deployment automatically (when using the command)? I dont think there is a need to set these manually rather than pulling them from the .env file and setting them automatically
a quick search in the docs lead me to this
@prof_mark No, they should not be passed to the server environment. You need to set them in your different environments manually. This also allows you to define them per environment. If for example you set up a staging/test environment with a different url
test.yourawesomeapp.com
you would then use this url on your staging server and yourawesomeapp.com
on your production server. Similar with other keys you might want to differentiate between environments. And regarding the fly deploy
command: At some point you will want to setup some sort of CI/CD, where the deployment is triggered by for example github actions. So your local .env
file would be lost anyways.Ah I see, that makes sense, thank you!
I wonder if it might make sense to had a flag in fly deploy for beginners like me who are using opensaas - the flag could just tell the deployment script to grab all my env values and run the command with these values for me.
You write yourself a script that can do that for you smth like this
^ this is just what I use and I don't guarantee it's the best and it will always work
Lol that works too haha
Wohooo @prof_mark, you just became a Waspeteer level 2!