HOSTING / LESSON 15
Serverless Functions.
Adding backend logic to your site

Loading the lessonβ¦
Loading activitiesβ¦
HOSTING / LESSON 15
Adding backend logic to your site

Loading the lessonβ¦
Loading activitiesβ¦
Up to now we've used Vercel to host websites. But what happens when your website needs to do something on the server? Maybe process a form, talk to a database, or hide an API key? That's exactly where serverless functions come in.
Let's clear up the name first, because it's kinda misleading. There are definitely servers involved. "Serverless" just means YOU don't have to manage them.
Think about tap water. You turn on the faucet and water comes out. You don't think about the pipes, the pumps or the treatment plant, because somebody else handles all of that. You just use the water.
Serverless works the same way. You write some code. When somebody needs it to run, Vercel's servers run it. When nobody needs it, those servers go do other stuff. You manage exactly nothing.
Serverless functions let you add backend logic to a frontend project. Here's when you'd reach for one.
Some services hand you API keys that have to stay secret. You can't put them in frontend code, because anybody can look at it. A serverless function keeps the key tucked away on the server.
When someone fills out your contact form, that data has to go somewhere. A serverless function can catch the form and send an email or save it to a database.
Databases usually need passwords and shouldn't be hit directly from the browser. A serverless function acts as the secure middleman.
Maybe you need to combine data from a few sources or reshape it. A serverless function can do that work before anything gets sent to the browser.
Vercel makes this RIDICULOUSLY simple. Here's the basic idea.
In a Next.js project, you create a file in the app/api folder (or pages/api for older projects). Each file becomes an API endpoint.
Inside the file you write a function that handles requests. It takes in data, does something with it and sends back a response.
When you deploy to Vercel, it turns these files into serverless functions automatically. Zero extra setup.
Your frontend calls these endpoints like any other API. The function runs, returns its data and shuts down.
Here's what a basic serverless function looks like in Next.js.
That creates an endpoint at /api/hello. Visit that URL and you get this back.
Yep, that's it. That's a complete serverless function.
Say you want to grab the weather, and the weather API needs a secret key. Here's how you'd keep it hidden.
The browser never sees your API key. It just calls /api/weather and gets the weather back.
In a Next.js project, your API routes live in one specific spot.
The folder structure turns into the URL path. A file at app/api/users/route.js creates an endpoint at /api/users.
Serverless functions can handle different kinds of requests.
Fetch data, like viewing a webpage.
Send data, like submitting a form.
And you can have both in the same file.
From your website's JavaScript, you call these endpoints the same way you'd call any other API.
Serverless functions come with a few limits to keep in mind.
As of October 2026, a function on the free Hobby plan can run for up to 5 minutes before Vercel cuts it off with a timeout error. Paid plans can go longer. Most functions finish in milliseconds anyway, so you'll only bump into this with slow jobs (and those belong on Railway, more on that below).
Functions get a limited amount of memory, 2 GB on the Hobby plan. For most things you'll build, that's plenty.
The Hobby plan includes 1 million function runs and 4 hours of active CPU time a month. For personal projects and learning, you won't hit that. These numbers change, so check vercel.com/pricing when it matters.
Rule of thumb... Vercel is great for frontend and quick backend tasks. When the backend work gets heavy, send it to Railway.
app/api/You can deploy, preview, configure and add backend logic now. There's one more common job left, which is hooking up your own custom domain. Let's do that next!