Deploy a custom API on Nhost. Also called: Lambda, Serverless Functions, Cloud Functions etc.
To deploy the custom API you must:
nhost init
api/
directory at the root of your project.Your API will be automatically deployed for every push to the master
or the main
branch of your repository.
module.exports = (req, res) => {
const { name = 'World' } = req.query;
res.status(200).send(`Hello ${name}!`);
};
The folder structure inside the api/
folder will resolve the endpoints.
If you're deploying your frontend to Vercel you can add
api
to.vercelignore
so the API-folder does not conflict with Vercel's deployment.
Given this folder and file structure:
|____api
| |____index.js
| |____users
| | |____index.js
| | |____active.js
| |____my-company.js
The following endpoints will be available:
https://api-str.nhost.app/ - (api/index.js)
https://api-str.nhost.app/users - (api/users/index.js)
https://api-str.nhost.app/users/active - (api/users/active.js)
https://api-str.nhost.app/my-company - (api/my-company.js)
System level environment variables are available:
NHOST_HASURA_URL - (Hasura URL)
NHOST_HASURA_ADMIN_SECRET - (Hasura admin secret)
NHOST_HBP_URL - (HBP URL for auth and storage)
NHOST_CUSTOM_API_URL - (Custom API URL)
NHOST_WEBHOOK_SECRET - (Random secret string. Can be used to authenticate requests from Hasura to the Custom API)
You can access each environment variable via process.env.<name>
ex: process.env.NHOST_HASURA_URL
.
All your production environment variables added under Settings -> Environment variables are also added to the custom API.
For local development all system environment variables will point to the local development URLs.
Logs are not available yet.
Use the Nhost CLI's nhost dev
to start the Custom API locally (together with a full Nhost backend).
TypeScript is supported.
import { Request, Response } from "express";
export default (req: Request, res: Response) => {
const { name = 'World' } = req.query;
res.status(200).send(`Hello ${name}!`);
};
We use Google Cloud Run to deploy the Custom API. For now we only allow JavaScript. In the future you will be able to bring your own Dockerfile with any language.