Next.js Discord

Discord Forum

Share memory between server actions (paypal example)

Unanswered
Weevil parasitoid posted this in #help-forum
Open in Discord
Avatar
Weevil parasitoidOP
Hi,

We're using Next.js and PayPal to handle payments in our project, and we’d like to clarify something. We understand that server actions and route handlers are Lambda functions executed in an edge runtime, which is great. However, PayPal requires us to make an API request using two specific environment variables to obtain a secret bearer token. This token is necessary for further requests, and when it expires or stops working, we’ll need to regenerate it to get a new one.

Currently, we are using an in-memory cache, so a server action calls the authentication endpoint once. However, this approach is inefficient because the token can be used for multiple actions. As we mentioned, server actions are Lambda functions, meaning they run with a fresh memory on each invocation, and storing a global variable isn't feasible.

Given this, what solutions do you recommend for storing and reusing the token efficiently?

We already use Redis for rate limiting, but we are hesitant to store the token there due to security concerns.

2 Replies

Avatar
Weevil parasitoidOP
Interesting
Avatar
West African Lion
Cloudflare D1 or KV might be interesting to investigate here. Since you can bind these in your Next.js app without need for an external backend, and you stay within the Cloudflare network. I know for KV there is some replication lag between regions. Not sure if this is also the case for D1. So this is good to keep in mind.