Netlify Functions
Build scalable, dynamic applications
Deploy server-side code that works as API endpoints, runs automatically in response to events, or processes more complex jobs in the background.
Write
Code your functions with JavaScript, TypeScript, or Go and drop them into your project’s netlify/functions folder. Everything else is automatic.
Deploy
Netlify deploys the functions you write as full API endpoints. Functions receive request context or event data, and return data back to your frontend.
Run
Trigger functions directly from your frontend through JavaScript or call them from other services via webhooks.
Use cases
Serverless Functions in action
"const API_ENDPOINT = 'https://cat-fact.herokuapp.com/facts';
export default async (request, context) => {
try {
const response = await fetch(API_ENDPOINT);
const data = await response.json();
return Response.json({ data });
} catch (error) {
console.log(error);
return Response.json({ error: 'Failed fetching data' }, { status: 500 });
}
}; const API_ROOT = 'https://api.unsplash.com';
export default async (request, context) => {
try {
const accessKey = process.env.ACCESS_KEY;
const doggoEndpoint = `\${API_ROOT}/photos/random?client_id=\${accessKey}&count=10&collections='3816141,1154337,1254279'`;
const response = await fetch(doggoEndpoint);
const images = await response.json();
return Response.json({ images });
} catch (error) {
console.log(error);
return Response.json({ error: 'Failed fetching images' }, { status: 500 });
}
}; import formData from "form-data";
import Mailgun from "mailgun.js";
import Airtable from "airtable";
const sendThankYouEmail = async ({ email }) => {
console.log('Sending the email');
const { MG_API_KEY: apiKey, MG_DOMAIN: domain } = process.env;
const mailgun = new Mailgun(formData).client({
username: 'api',
key: apiKey,
});
const mailData = {
from: \`Stefan Judis stefan@\${domain}\`,
to: email,
subject: 'Thank you for your interest',
text: "I'll come back to you asap!",
};
await mailgun.messages.create(domain, mailData);
};
const saveUser = async ({ name, email, message }) => {
const { AT_API_KEY: apiKey, AT_BASE, AT_TABLE } = process.env;
Airtable.configure({
apiKey,
});
const base = Airtable.base(AT_BASE);
const table = base(AT_TABLE);
await table.create({ name, email, message });
};
export default async (request, context) => {
try {
const data = await request.json();
await sendThankYouEmail(data);
if (data.receiveUpdates) {
await saveUser(data);
}
return Response.json({ message: "Let's become serverless conductors!!!" });
} catch (error) {
console.log(error);
return Response.json({ error: 'Failed sending email' }, { status: 500 });
}
}; // Simple Contact Form Spam Filter
export default = async (request, context) => {
// 1. Parse the form
let body
try {
body = await request.json()
}
catch (e) {
console.log(event)
return new Response(`[ERROR] Invalid JSON - ${e.message}`, { status: 400 })
}
// 2. Filter
if (!body.data.name ||
!body.data.message)
{
const errorMessage = '[SPAM DETECTED] Required fields not defined.'
console.log(errorMessage)
return new Response(errorMessage, { status: 200 })
}
// 3. Forward data to webhook (ie, send email)
try {
const res = await fetch(process.env.ZAPIER_CONTACT_FORM_WEBHOOK, {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' }
})
return new Response("Success")
} catch (error) {
console.error("[ERROR] Problem with request:", error)
return new Response("[ERROR] Problem with request", { status: 400 })
}
} Get shorter build times and faster page load speeds with on-demand builders.
Features
All part of one amazing workflow
Moving all LiveChat websites to Netlify has empowered our Web Dev Team to iterate fast and independently.
Deploy & collaborate
Netlify Workflow
Work as a team, right in Git. Netlify neatly deploys every resource to the right place, from global static assets to serverless functions.
Learn more about workflows Build & integrate
Netlify Build
The Git workflow for web development. Build, deploy, and manage modern web projects.
Learn more about faster builds Execute at the edge
Netlify Edge
A fast, resilient network for web apps. Connected to your development workflow and designed to handle the most complex tasks - or even to run your own custom logic.
Learn more about Netlify Edge Extensions
Addons & Integrations
Add powerful capabilities to every build and integrate easily with other SaaS services. Find the plugins you need from the community or even create your own.
Learn more about integrationsStart building on Netlify
Join millions of developers and teams bringing their ideas online.