Netlify Serverless Functions Cheat Sheet
Netlify Serverless Functions Cheat Sheet
Cheat Sheet
By @jamesqquick - jamesqquick.com
function.
💡 The name of your Serverless Function file will be the name of the
serverless function itself.
Synchronous handlers must call the callback function with an error as the first
parameter (if applicable), and the second an object that includes HTTP statusCode
and body .
👍
statusCode: 200,
body: JSON.stringify({msg: ' '})
})
}
Asynchronous handlers must be tagged as async and do not need to call the
callback function. It's fine to remove the callback parameter if you aren't using it.
👍
statusCode: 200,
body: JSON.stringify({msg: ' '})
}
}
If you need to access the request JSON body, you have to parse it with
JSON.parse() .
[build]
functions = "functions"
[[redirects]]
from = "api/*"
to= ".netlify/functions/:splat"
status = 200
Extra Tips
🔥 Use Environment Variables (Full Video)
but make sure you ignore this file in your .gitignore file
.env
Lastly, require the dotenv packages and grab the variables you need.
require('dotenv').config()
const secretKey = process.env.SECRET_KEY
👍
exports.handler = async(event, context) => {
return formattedReturn(200, {msg: ' '});
}