Add API Endpoints to a Website in Launch
Launch allows you to write Cloud Functions to create API endpoints.
This step-by-step guide lets you add API endpoints to a website in Launch.
Prerequisites
- Contentstack account
- Access to Launch for your organization
Steps for Execution
Follow the steps given below to write a Cloud Function that can be used to add API endpoints to a website in Launch.
- Create a folder named /functions under a source folder in your project’s root folder.
- Create a JavaScript file to code your functions and then save the file to the /functions folder.
Example:
// functions/users.js export default function handler(request, response) { const users = [ {name: "Jack", age: "25"}, {name: "Rick", age: "28"}, {name: "Jane", age: "34"}, ]; response.status(200).send(users); } - Zip the source folder that contains the /functions folder.
- Deploy your project in Launch using one of the following methods:After successful deployment, you will see the Cloud Functions displayed in the log:The function endpoint is determined by its file path relative to /functions. The function runs when you visit the path: /users.
- Click the icon next to the URL in the Domains section to open the deployed website.In this example, the URL of the API endpoint will be https://sample-test-api.devsampleapp.com/users.
- On sending a request to the API Endpoint, you will see the following response:
[ { "name": "Jack", "age": "25" }, { "name": "Rick", "age": "28" }, { "name": "Jane", "age": "34" } ]
More articles in "Launch How to Guides"
Deploy a Project from a Monorepo on Launch
Learn more
Disable Automatic Redeployment
Learn more
Host a Static Site on Launch
Learn more
Redeploy Automatically when Content is Published on CMS
Learn more
Setup Production and Non-Production Environment
Learn more
Auto-populate Environment Variables from a Linked Stack
Learn more
Repair GitHub Connection for Projects
Learn more
Repair Git Provider Connection for Projects
Learn more
Host any SSR Framework on Launch
Learn more
Trigger Deployments on Launch Platform based on Tags/Releases
Learn more
Caching Guide for Contentstack Launch
Learn more
Handling Next.js RSC Issues on Launch
Learn more
IP-based Access Control Using Edge Functions in Launch
Learn more
Blocking AI Crawlers
Learn more
Blocking Default Launch Domains From Google Search
Learn more
Can I Run a Docker Container on Launch?
Learn more
