Create Netlify functions, use environment variables, install third party packages, pass data to functions and test locally and on live remote
Summary
There are functions and there are edge functions. Both are serverless functions, with edge function being served from CDN server locations closest to the user ref.
netlify.toml
or .nvmrc
).jsx
, .tsx
are also supported for SSR (server-side-handling)Function or Edge Function? Which one to choose? Personally, i always go with edge-functions
because they’re simpler and i don’t have to worry about module formats. The only reason i would choose functions
is if i wanted to write Go functions
netlify
├── edge-functions <-- Directory for Edge Functions
│ └── fetch_users.ts
│ └── foo.js
│ └── bar.tsx
│ └── baz.jsx
└── functions <-- Directory for Functions
│ └── send_email.ts <-- ES module if type:module in package.json, otherwise CommonJS
│ └── send_email.mts <-- ES module
│ └── send_email.cts <-- CommonJS module
1# netlify.toml
2
3[build]
4 functions = "netlify/functions"
5 edge_functions = "my-custom-directory"
6
7[functions]
8 node_bundler = "esbuild"
1# .nvmrc
2lts/*
Creating both types of functions is the same, just the folder name changes. Creat a file with the relevant file extension (.ts/.mts/.cjs for Functions or .ts/.js/.tsx/.jsx for Edge Functions) in the relevant directory (inside BASE_DIR/netlify/functions
for Functions or in BASE_DIR/netlify/edge-functions
for Edge Functions)
The default directories are YOUR_BASE_DIRECTORY/netlify/edge-functions
and YOUR_BASE_DIRECTORY/netlify/functions
. You can change these defaults in netlify.toml
or the Netlify dashboard
1# netlify.toml
2
3[build]
4 base = '/'
5 functions = "netlify/functions"
6 edge_functions = "my-custom-directory"
7
8[functions]
9 node_bundler = "esbuild"
Create the directory and the file
1mkdir -p netlify/edge-functions
2cd netlify/edge-functions
3
4touch testfunction.ts
Edge Functions support using npm modules inside your functions
1npm install @sendgrid/mail
In order to run the functions, we need to install a bunch of packages. Node should already be installed
1npm install netlify-cli --global
If you’re using TypeScript and want type-safety inside your functions, install @netlify/edge-functions
and/or @netlify/functions
1npm install @netlify/edge-functions
2npm install @netlify/functions
1# test locally
2netlify dev
You can set environment variables from the Netlify dashborad area.
Note that environment variables declared in a Netlify configuration file (netlify.toml
) are not available to functions. process.env
vars are also not available. You would use Netlify.env.get()
instead to use these variables inside your function
1export const OWM_API_KEY = Netlify.env.get("OPENWEATHER_API_KEY")
The default URLs for Netlify Functions are:
# Directory
YOUR_BASE_DIRECTORY/netlify/functions
# Local (Netlify Dev)
http://localhost:8888/.netlify/functions/<FUNCTION NAME>
# Deployed
https://<YOUR DOMAIN>/.netlify/functions/<FUNCTION NAME>
and for Netlify Edge Functions:
# Directory
YOUR_BASE_DIRECTORY/netlify/edge-functions
# Local (Netlify Dev)
http://localhost:8888/<FUNCTION PATH>
# Deployed
yoursitename.netlify.app/<FUNCTION NAME>
Check the URL for the function. It is most likely not hitting the right URL and the function is not being called.
Check if you are actually returning a response inside your function