Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-api

App Script - Alternative to using UrlFetchApp


Good afternoon

Trying to access API

https://developer-portal.driver-vehicle-licensing.api.gov.uk/apis/vehicle-enquiry-service/code-examples.html#ves-api-code-examples

from App Script

Have accessed other API's in the past & used UrlFetchApp, but does not seem to be supported by this API

Not sure how to use one of the other options

Thanks Simon


Solution

  • I believe your goal is as follows.

    • From your provided official document, you want to convert the following curl command to Google Apps Script.

      curl --location --request POST 'https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles' \
      --header 'x-api-key: REPLACE WITH YOUR API KEY' \
      --header 'Content-Type: application/json' \
      --data '{"registrationNumber": "AA19AAA"}'
      
    • You have already confirmed that the above curl command worked fine.

    In this case, how about the following sample script?

    Sample script:

    function myFunction() {
      const url = "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry/v1/vehicles";
      const data = { "registrationNumber": "AA19AAA" };
      const res = UrlFetchApp.fetch(url, {
        method: "post",
        headers: { "x-api-key": "REPLACE WITH YOUR API KEY" },
        contentType: "application/json",
        payload: JSON.stringify(data)
      });
      console.log(res.getContentText());
    }
    

    Note:

    • In this sample, it supposes that you have already confirmed that the sample curl command works fine with your values. Please be careful about this.

    Reference: