Netlify API Axios Get Request

I’m working on an app that reads form submissions and builds contact cards from that data. I have a build hook triggering this node file to run. I feel like I’m close but I’m getting a 401. Can anyone help me out here on how to make an Axios request to the Netlify API?

const axios = require('axios');
var fs = require('fs');

axios.request({
    url: "https://api.netlify.com/api/v1/forms/5ff0f18977371a0007907109/submissions", //form data needed 
    method: "get",
    baseURL: "https://app.netlify.com/authorize", //Authorization URL
    auth: {
        username: "$", // client_id
        password: "$" // client_secret
    }
}).then(function (response) {
    console.log(response);
    //create a file
    fs.writeFile(`public/${Fname}-${Lname}.vcf`,
        `
            BEGIN:VCARD
            ...
            N:${Lname};${Fname};;;
            ...
            END:VCARD
            `
        , function (err) {
            if (err) throw err;
            console.log('Saved!');
        });
}).catch(function (error) {
    console.log(error);
});

Here is the production code:

Hi, @BillyMitchell. The 401 is an unauthorized status code so for that I’m guess the issue is the username and password.

First, I would recommend using an API key instead of the username/password to access the Netlify API:

https://docs.netlify.com/api/get-started/#authentication

You can create the personal access token for the API here (only when logged in):

https://app.netlify.com/user/applications#personal-access-tokens

This will hopefully resolve the 401 issue. However, I also see a second likely pitfall once the 401 is resolved.

This code above appears to write the vCard to the local filesystem.

Serverless functions are not running on the CDN node itself. This means serverless functions can neither read from nor write to the site’s deployed files. You cannot modify a deployed site by writing to the functions filesystem. Only a new deploy can modify the site.

Is that what the code above is attempting to do?

Thanks for the response! I was able to figure it out by toying with Postman. Form submission triggers a build hook, which runs this code. The only problem now is that I’m getting the error described here:

“git ref refs/heads/master does not exist or you do not have permission”

const fetch = require('node-fetch');
var fs = require('fs');
require("dotenv").config()

const formID = "5ff0f18977371a0007907109"
const placeholder = "NA"

var requestOptions = {
method: 'GET',
redirect: 'follow'
};

fetch(`https://api.netlify.com/api/v1/forms/${formID}/submissions?access_token=${process.env.NETLIFY_ACCESS_TOKEN}`, requestOptions)
.then(response => response.text())
.then(function (data) {
    data = JSON.parse(data)
    data.forEach(submission => {
        fs.writeFile(`public/${submission.first_name}-${submission.last_name}.vcf`,
            `
                BEGIN:VCARD
                VERSION:3.0
                PRODID:-//Apple Inc.//iPhone OS 13.1.3//EN
                N:${submission.first_name};${submission.last_name};;;
                ...
                END:VCARD
                `
            , function (err) {
                if (err) throw err;
                console.log('Saved!');
            })
    })
})

.catch(error => console.log('error', error))

Where is the “git ref refs/heads/master” error occurring, @BillyMitchell? Is it in the build and deploy logs for a new deploy being triggered? If so, what is the deploy id where you see that?

Not sure what the deploy id is.
https://vcard.billymitchell.design/

3:20:02 PM: Build ready to start
3:20:04 PM: build-image version: 53b83b6bede2920f236b25b6f5a95334320dc849
3:20:04 PM: build-image tag: v3.6.0
3:20:04 PM: buildbot version: e26592b49d9d56b21349effe1b8fab4d50845f11
3:20:04 PM: Fetching cached dependencies
3:20:04 PM: Failed to fetch cache, continuing with build
3:20:04 PM: Starting to prepare the repo for build
3:20:05 PM: git ref refs/heads/master does not exist or you do not have permission
3:20:05 PM: Failing build: Failed to prepare repo
3:20:05 PM: Failed during stage 'preparing repo': git ref refs/heads/master does not exist
3:20:05 PM: Finished processing build request in 741.096423ms

hi there,

this error indicates we werent able to connect to your git repo correctly.

is the URL and the branch name correct?