Back

[Solved] requests using functions

  • 0
  • Self Hosted
  • Web
fafa
2 Mar, 2024, 21:34

Let's say all my api endpoints are on appwrite functions, all they need is the session token. Normally I would login with user/pass and save the token and use that token sent in a header on the server

How would this work using functions? Or do I have to create a JWT token and send that token to the function?

TL;DR
Developers originally mentioned issues with compatibility between RC 5 and Appwrite 1.4. Problem resolved with the realization that the desired feature already exists. To send a "Create execution" request, utilize the JWT token in the headers section. This makes the process streamlined. Developers express preference for avoiding JWT token generation due to frequent requests. Instead, they propose utilizing the user's token for server communication in Appwrite functions for better efficiency.
fafa
2 Mar, 2024, 21:36

I'd rather not generate a JWT token, because I make a lot of requests. Needing to make a new JWT token every time is a little.. too much extra work, having to check, creating a new one on runtime etc. It's not efficient, but having the token of the user and sending that token to for example the function, that's a lot faster and simpler

Steven
2 Mar, 2024, 21:37

1.5 might be good for you so that you can get a secret and then use that for subsequent requests

fafa
2 Mar, 2024, 21:37

oh perfect, i did see the createSession, is that it?

fafa
2 Mar, 2024, 21:38

and does this secret have an expiration date? or just as long as the session is active?

Steven
2 Mar, 2024, 21:41

It would be whatever you set session length limit to be

Steven
2 Mar, 2024, 21:41

Just like the usual email password session and any other session

fafa
2 Mar, 2024, 21:45

Nice! Thanks :)

fafa
2 Mar, 2024, 21:45

[Solved] requests using functions

fafa
2 Mar, 2024, 22:50

@Steven It turns out this feature already exists!

Or might it be because of using the RC-5..?

Anyway, if you send a "Create execution" request, it sends you the following req:

TypeScript
{
   "bodyRaw":"",
   "body":"",
   "headers":{
      "host":"dash.fayevr.dev",
      "x-appwrite-trigger":"http",
      "x-appwrite-user-id":"USERIDHERE",
      "x-appwrite-user-jwt":"JWTTOKENHERE",
      "x-appwrite-continent-eu":"false",
      "connection":"keep-alive",
      "content-length":"0"
   },
   "method":"GET",
   "host":"dash.fayevr.dev",
   "scheme":"http",
   "query":{
      
   },
   "queryString":"",
   "port":80,
   "url":"http://dash.fayevr.dev/getUserSelf",
   "path":"/getUserSelf"
}

The "X-Appwrite-User-JWT" has the JWT token needed, this makes it super easy by just saying:

TypeScript
client
    .setEndpoint(`${process.env.APPWRITE_API_URL}/v1`)
    .setProject(`${process.env.APPWRITE_PROJECT_ID}`)
    .setJWT(req.headers['x-appwrite-user-jwt'] || '');
fafa
2 Mar, 2024, 22:50
D5
2 Mar, 2024, 22:59

Could be. RC 5 is not ready to use or compatible at all with Appwrite 1.4

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more