Creating a simple REST API using Google Sheets as your database is a fantastic way to manage data in an easily accessible and cloud-based manner. In this tutorial, we'll go through the entire process of setting up the API, including how to handle GET
, POST
, PUT
, and DELETE
requests.
Step 1: Set Up Your Google Sheet
- Open a new or existing Google Sheets document.
- Create a sheet with the following columns:
name
,uid
,project_name
. For example:name | uid | project_name ---------------------------------------------------- Shubhadip Bhowmik | 22BCA10032 | Hirequest Website Deepasha Rani | 22BCA10016 | Portfolio Website Sanya Sharma | 22BCA1006 | Mobile App Design ...
- Make sure the data is organized and that the first row is used as the header.
Step 2: Open Google Apps Script
- In the Google Sheets document, click on Extensions > Apps Script.
- This will open the Google Apps Script editor, where you can write the code for the REST API.
Step 3: Write the API Code
Copy the following code into the script editor:
// 📥 GET Request - Fetch All Data
function doGet(e) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = sheet.getDataRange().getValues();
const headers = data[0];
const rows = data.slice(1).map((row) => {
let obj = {};
headers.forEach((header, i) => (obj[header] = row[i]));
return obj;
});
return respond({ status: "success", data: rows });
}
// 📤 POST Request - Add New Data
function doPost(e) {
if (!e || !e.postData || !e.postData.contents) {
return respond({ status: "error", message: "Invalid request body" });
}
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = JSON.parse(e.postData.contents);
if (data.name && data.uid && data.project_name) {
sheet.appendRow([data.name, data.uid, data.project_name]);
return respond({ status: "added", data });
} else {
return respond({ status: "error", message: "Missing fields" });
}
}
// ✏️ PUT Request - Update Existing Data
function doPut(e) {
if (!e || !e.postData || !e.postData.contents) {
return respond({ status: "error", message: "Invalid request body" });
}
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = JSON.parse(e.postData.contents);
const allData = sheet.getDataRange().getValues();
for (let i = 1; i < allData.length; i++) {
if (allData[i][1] === data.uid) {
// UID is in the second column
sheet
.getRange(i + 1, 1, 1, 3)
.setValues([[data.name, data.uid, data.project_name]]);
return respond({ status: "updated", data });
}
}
return respond({ status: "error", message: "UID not found" });
}
// ❌ DELETE Request - Delete Data by UID
function doDelete(e) {
if (!e || !e.postData || !e.postData.contents) {
return respond({ status: "error", message: "Invalid request body" });
}
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const data = JSON.parse(e.postData.contents);
const allData = sheet.getDataRange().getValues();
for (let i = 1; i < allData.length; i++) {
if (allData[i][1] === data.uid) {
// UID is in the second column
sheet.deleteRow(i + 1);
return respond({ status: "deleted", uid: data.uid });
}
}
return respond({ status: "error", message: "UID not found" });
}
// ✅ Response Formatter
function respond(payload) {
return ContentService.createTextOutput(JSON.stringify(payload)).setMimeType(
ContentService.MimeType.JSON
);
}
This script handles four types of HTTP requests:
- GET - Fetches all data from the Google Sheet.
- POST - Adds new data to the Google Sheet.
- PUT - Updates existing data by matching
uid
. - DELETE - Deletes data by matching
uid
.
Step 4: Deploy as Web App
To make your script accessible as a web API, follow these steps:
- In the Apps Script editor, click Deploy > Manage Deployments.
- Click New Deployment and choose Web app.
- Under Execution as, select Me.
- Under Who has access, select Anyone (this makes the API accessible without authentication).
- Click Deploy. You'll be prompted to review permissions; grant the necessary access.
- Once deployed, you’ll receive a URL. This is your API endpoint, for example:
https://script.google.com/macros/s/AKfycbwT4uAUBWw4Cxyz1234567890abcdefghijkl/exec
Step 5: Test the API Using Postman
Postman is a tool that makes it easy to test your API. Here's how you can use it:
Test GET Request (Fetch Data):
- Open Postman.
- Set the request type to
GET
. - Enter the URL of your deployed API endpoint.
- Click Send. You should get a response with the data from your Google Sheet.
Test POST Request (Add New Data):
- Set the request type to
POST
. - Enter the URL of your deployed API endpoint.
- Go to the Body tab in Postman and select raw and JSON.
- Enter the following JSON data:
{ "name": "Swapnil Kumar", "uid": "22BCA10047", "project_name": "CU Foodz" }
- Click Send. If successful, you should receive a response indicating that the data was added.
Test PUT Request (Update Data):
- Set the request type to
PUT
. - Enter the URL of your deployed API endpoint.
- Go to the Body tab and enter the following JSON data:
{ "name": "Swapnil Kumar", "uid": "22BCA10047", "project_name": "Guitar Wala" }
- Click Send. The response should confirm the update.
Test DELETE Request (Delete Data):
- Set the request type to
DELETE
. - Enter the URL of your deployed API endpoint.
- Go to the Body tab and enter the following JSON data:
{ "uid": "22BCA10032" }
- Click Send. The response should confirm that the data has been deleted.
Step 6: Done!
Now you have a fully functional REST API backed by Google Sheets! You can interact with your sheet by sending GET
, POST
, PUT
, and DELETE
requests using Postman or any other HTTP client.
In this tutorial, we showed how to use Google Apps Script to create a simple REST API backed by a Google Sheet. You can now easily interact with this API to manage your data, whether it’s retrieving, adding, updating, or deleting records. This method is great for small projects and can serve as a foundation for more complex API systems.