Search code examples
node.jsgoogle-cloud-functionshttp-referer

NodeJS / Google Cloud Function — CSRF-verification failed response from server


I get a "CSRF-verification failed" response from the server.

I don't get why tho. I'm trying both using Google Cloud Function and running the code on my own machine.

Error Reponse

CSRF-verification failed. Try again.

Code

const axios = require('axios')
const to = require('await-to-js').default

const getAPI = () => {

    // basic auth
    const auth = {
        username: "API USER",
        password: "API PASSWORD"
    }

    const instance = axios.create({
        baseURL: "https://sandbox.billogram.com/api/v2/invoice",
        auth,
        headers: {
            Referer: "http://localhost:3000/"
        }
    })
    return instance
}

const createInvoice = async () => {
    const api = getAPI()
    const [err, res] = await to(
        api.post('invoice', {
            "items": [
                {
                    "description": "Item 1",
                    "quantity": 2,
                    "unit_price": 100
                }
            ],
            "currency": "SEK",
            "customer": {
                "first_name": "John",
                "last_name": "Doe",
                "email": "[email protected]",
                "phone": "555-555-5555"
            }
        })
    )
    if (err) return console.log('ERR', err.message, err.response.data)
    console.log(res.data)
}

createInvoice()
    .then((res) => {
        console.log(res)
    })

Question

  • Can you try to explain how come?

  • Also how to find the correct Referer from Google Cloud and local development machine?

Many thanks in advance!


Solution

  • It was me who relied on ChatGPT code too much. The API was updated and the API endpoint had been changed.