Basic REST API POST Request Using JSON


This how-to guide will cover making a POST request to an API using JSON data. For retrieving JSON data from an API and parsing it out, check out the Basic REST API GET Call Using JSON how-to guide.

The Basics

With Google Apps Scripts, you can use a built-in library called UrlFetchApp which contains a method called .fetch(). This .fetch() method accepts two parameters - the first is the URL you want to make the API request against and the second is an object for your "options" which could include the headers options and any other options you need. In this case, you'll want to make sure that the content-type is set to "application/json", that the method is set to "post", and that you're passing a payload through - payloads are usually going to be an object.

UrlFetchApp.fetch(url, {headers: {"Content-Type": "application/json"}, method: 'post', payload: payload})

Was it Successful?

After you've made a POST request to an API, there's usually a response to let you know if it was successful. If there isn't, I'd be weary of that API... Just saying..

In order to read the result of your POST request, you'll need to parse the data. In a nutshell, you'll do a JSON.parse() against the content text of the response. For a more in-depth explanation, check out the Basic REST API GET Call Using JSON how-to guide.

const response = UrlFetchApp.fetch(url, {headers: {"Content-Type": "application/json"}, method: 'post', payload: payload}) const responseData = response.getContentText()) const parsedData = JSON.parse(responseData)

Making it Easy to Use

Here's a pre-written function that helps you make a POST request to an API where the only thing you need to do is pass through the URL of the API Endpoint and the payload you're looking to send up to the API.

function basicJsonApiPostRequest(url, payload) { const result = JSON.parse(UrlFetchApp.fetch(url, {headers: {"Content-Type": "application/json"}, method: 'post', payload: payload}).getContentText()) return result } // // To run it, just call the function and pass in the API Endpoint along with your payload const payload = { name:"test", salary:"123", age:"23" } basicJsonApiPostRequest('https://dummy.restapiexample.com/api/v1/create', payload)

Being able to make an API request in Google Apps Script is a powerful tool to have in your arsenal. This basic post request will help you push your Google Sheets data into order applications and databases, as long as you know the endpoint the data should go to and how the payload should be formatted.

Keep in mind that you may need to include an API Key in your url params or an 'Authorization' variable to your headers if the API you're connecting to requires authentication. Some APIs require OAuth2.0 authentication, which is something we cover in our video guide for accessing the Google Search Console API.

thumbnail

NitroGAS Chrome Extension

Want to reference this code snippet right from your Google Apps Script Project? Check out our NitroGAS Chrome Extension. This tool will help you build your scripts faster than you could imagine. The tool itself and access to the code snippets are 100% FREE - Happy Coding!

Get the Extension