What is @netlify/functions?
@netlify/functions is an npm package that allows developers to create serverless functions that can be deployed on Netlify. These functions can handle HTTP requests, interact with APIs, and perform various backend tasks without the need for a dedicated server.
What are @netlify/functions's main functionalities?
HTTP Request Handling
This feature allows you to handle HTTP requests and send responses. The code sample demonstrates a simple function that returns a 'Hello, World!' message.
const { Handler } = require('@netlify/functions');
const handler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello, World!' })
};
};
module.exports = { handler };
Environment Variables
This feature allows you to access environment variables within your serverless functions. The code sample demonstrates how to read an environment variable and return its value in the response.
const { Handler } = require('@netlify/functions');
const handler = async (event, context) => {
const secret = process.env.SECRET_KEY;
return {
statusCode: 200,
body: JSON.stringify({ secret })
};
};
module.exports = { handler };
API Integration
This feature allows you to integrate with external APIs. The code sample demonstrates how to fetch data from an external API and return it in the response.
const { Handler } = require('@netlify/functions');
const fetch = require('node-fetch');
const handler = async (event, context) => {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return {
statusCode: 200,
body: JSON.stringify(data)
};
};
module.exports = { handler };
Other packages similar to @netlify/functions
aws-lambda
AWS Lambda is a serverless computing service provided by Amazon Web Services. It allows you to run code without provisioning or managing servers. Compared to @netlify/functions, AWS Lambda offers more extensive integration with other AWS services but may require more configuration and setup.
vercel
Vercel (formerly known as Zeit Now) is a platform for serverless functions and static site hosting. It provides a similar serverless function capability as @netlify/functions but is tightly integrated with the Vercel platform. It offers easy deployment and scaling of serverless functions.
firebase-functions
Firebase Functions is a serverless framework provided by Google Firebase. It allows you to run backend code in response to events triggered by Firebase features and HTTPS requests. Compared to @netlify/functions, Firebase Functions is more integrated with the Firebase ecosystem, making it a good choice for apps already using Firebase services.
JavaScript and TypeScript utilities for Netlify Functions.
Installation
npm install @netlify/functions
Usage
On-demand Builders
To use On-demand Builders, wrap your function handler with the builder
function.
-
With JavaScript:
const { builder } = require('@netlify/functions')
const handler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello World' }),
}
}
exports.handler = builder(handler)
-
With TypeScript:
import { builder, Handler } from '@netlify/functions'
const myHandler: Handler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello World' }),
}
}
const handler = builder(myHandler)
export { handler }
Scheduled Functions (currently in beta)
To use Scheduled Functions, wrap your function handler with the schedule
function.
-
With JavaScript:
const { schedule } = require('@netlify/functions')
exports.handler = schedule('5 4 * * *', async () => {
console.log("It's 04:05 AM!")
})
-
With TypeScript:
import { schedule } from '@netlify/functions'
export const handler = schedule("5 4 * * *", async () => {
console.log("It's 04:05 AM!")
})
TypeScript typings
This module exports typings for authoring Netlify Functions in TypeScript.
import { Handler } from '@netlify/functions'
const handler: Handler = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({ message: 'Hello World' }),
}
}
export { handler }
The following types are exported:
Handler
HandlerCallback
HandlerContext
HandlerEvent
HandlerResponse
Contributors
Please see CONTRIBUTING.md for instructions on how to set up and work on this repository. Thanks
for contributing!