What the difference?

What's the difference between these? You can use both anywhere, but which one and why should I prefer? If I'd have a seperate Rest API, then I wouldn't be able to use the first one at all, so would I lose out on anything?
const response = await auth.api.getSession({
headers: await headers(),
});
const response = await auth.api.getSession({
headers: await headers(),
});
and
const response = await authClient.getSession({
fetchOptions: { headers: await headers() },
});
const response = await authClient.getSession({
fetchOptions: { headers: await headers() },
});
Solution:
auth.api is for server-side usage. it calls the Better Auth API endpoints. authClient.getSession is for client-side usage. if you’re using a separate API, you’d typically lose access to the direct server-side method since it isn’t exposed over HTTP in the same way. auth.api provides a bit more performance efficiency on the server by avoiding extra HTTP overhead, while the client method is optimized for browser or external API calls....
Jump to solution
3 Replies
Solution
begot
begot6h ago
auth.api is for server-side usage. it calls the Better Auth API endpoints. authClient.getSession is for client-side usage. if you’re using a separate API, you’d typically lose access to the direct server-side method since it isn’t exposed over HTTP in the same way. auth.api provides a bit more performance efficiency on the server by avoiding extra HTTP overhead, while the client method is optimized for browser or external API calls.
Karl
KarlOP6h ago
So I should prefer the server-side usage, but I assume that the performance difference would be negligible in prod But thanks
begot
begot6h ago
yeah sure, its just nice to keep in mind. i do prefer using auth.api where i can

Did you find this page helpful?