How to create an AI app with Upstash, Next.js and Replicate
In this article, we will show you how to rate limit a Next.js app using Upstash Redis. We will be rebuilding lofianime.com, an app that generates AI images with a Stable Diffusion model on Replicate. We will focus predominantly on how to implement Upstash rate limiting with a Redis database. You can check out the original source code for this project on GitHub.
Why use Upstash?
To avoid high API costs incurred from using hosted AI models, it's imperative that we rate limit our API routes. Upstash provides an SDK that allows us to easily do this.
Getting Started
We will create a Next.js application and deploy it to Vercel. Alternatively, you can clone the lofianime repository from GitHub, or use one-click deployment from the repo's README file.
Project Setup
Create a new Next.js project. Make sure to enable the App Router
, and tailwindCSS
. We will use Javascript instead of Typescript for the sake of simplicity.
Install @upstash/ratelimit:
Install the Replicate Node SDK:
Database Setup
Create a Redis database using the Upstash Console or the Upstash CLI. Copy the UPSTASH_REDIS_REST_URL
and UPSTASH_REDIS_REST_TOKEN
variables and place them in the .env.local
file in the root directory of your Next project.
Replicate AI Setup
Create a Replicate account. Click on your profile icon and select “API Tokens”. Create a new API token and save it in your .env.local
file as REPLICATE_API_TOKEN
.
Making the API Route
Create a new API route called app/api/ai/route.js
. In this route, we will use Upstash Redis to create a rate limit as well as use the Replicate SDK to generate an image.
Your route should look like this:
Let's break this code down. We first create a Redis client using the Upstash SDK. We then use that client to create a RateLimit of 5 requests with a fixed window of 1440 minutes, or 24 hours. Then, we create an instance of Replicate for our image generation. Using Next.js route handlers, we define a function to handle POST
requests. In this example, we limit all requests for the app. However, by getting an email
or IP address
, we can rate limit on an individual visitor level. In our POST
function, we check if the user has remaining generations. If they have reached their limit, we return a response with a 429
HTTP status code and set the headers with the limit and the remaining amount.
If the user does have remaining generations, we grab the prompt from the request and run the Replicate model using the SDK. Then, we return the output of the model with the Upstash Redis limit data in the response headers.
Building the UI
In your app/page.js
file, add the following code:
Additionally, update your next.config.js
file in the root of the directory to look like this:
Let's break this code down. We created a simple React form that will submit the user prompt to our API route. When we receive a response, we set a state variable with the image URL and then render the image in the UI. Finally, we updated next.config.js
so that we can remotely load images from Replicate.
And it works! If we inspect the network tab of the browser developer tools, we can see the rate limit as well as the number of generations remaining in the headers of our API route response. If we reach the limit, the API will return a 429 status code and will not allow us to generate any more images.
Congratulations! You just created a Next.js app that generates images with AI that is rate limited using Upstash Redis. Not too bad, right?
Possible Next Steps
There are many enhancements we can add to make this project even better. Here are a few good examples if you're feeling lucky:
Add authentication and personal rate limiting
As mentioned above, we could add user authentication using a library such as Auth.js that would allow us to rate limit on an individual basis. We can create an identifier by using the user's email and use this to rate limit:
Make UI updates to show remaining generations
We could also create an API route that calculates how many generations are left for a user. Then, we can fetch that data in the UI to show the user if their limits were exceeded.
About the Author
Cameron Youngblood is a Full Stack Developer at Ampry Software and a contributor to Alpine Codex. lofianime.com Star this project on GitHub