Google Cloud Functions - Sample Functions
Code samples to help you build your Google Cloud Function. The pieces of code show what a Google Cloud Function implementation looks like in different programming languages.
We have included two sample functions for you to use to accelerate your function development:
A sample function request and the generated response to help you understand Fivetran's request and response format and how to use the different nodes to write your cloud function.
A real-world sample function to retrieve data from the Transport API.
Sample function request
Node.js
/**
* @param {!Object} req Cloud Function request context.
* @param {!Object} res Cloud Function response context.
*/
exports.handler = (req, res) => {
if (req.body.state === undefined) {
res.status(400).send('No state is defined!');
}
if (req.body.secrets === undefined) {
res.status(400).send('No secrets is defined!');
}
res.header("Content-Type", "application/json");
res.status(200).send(update(req.body.state, req.body.secrets));
};
function update(state, secrets) {
// Fetch records using api calls
let [insertTransactions, deleteTransactions, newTransactionsCursor] = apiResponse(state, secrets);
// Populate records and state
return ({
state: {
transactionsCursor: newTransactionsCursor
},
insert: {
transactions: insertTransactions
},
delete: {
transactions: deleteTransactions
},
schema : {
transactions : {
primary_key : ['order_id', 'date']
}
},
hasMore : false
});
}
function apiResponse(state, secrets) {
var insertTransactions = [
{"date":'2017-12-31T05:12:05Z', "order_id":1001, "amount":'$1200', "discount":'$12'},
{"date":'2017-12-31T06:12:04Z', "order_id":1001, "amount":'$1200', "discount":'$12'},
];
var deleteTransactions = [
{"date":'2017-12-31T05:12:05Z', "order_id":1000, "amount":'$1200', "discount":'$12'},
{"date":'2017-12-31T06:12:04Z', "order_id":1000, "amount":'$1200', "discount":'$12'},
];
return [insertTransactions, deleteTransactions, '2018-01-01T00:00:00Z'];
}
For more information about the fields in the request, see our request format documentation.
Generated response
The sample function request generates the following JSON response:
{
"state": {
"transactionsCursor": "2018-01-01T00:00:00Z"
},
"insert": {
"transactions": [
{
"date": "2017-12-31T05:12:05Z",
"order_id": 1001,
"amount": "$1200",
"discount": "$12"
},
{
"date": "2017-12-31T06:12:04Z",
"order_id": 1001,
"amount": "$1200",
"discount": "$12"
}
]
},
"delete": {
"transactions": [
{
"date": "2017-12-31T05:12:05Z",
"order_id": 1000,
"amount": "$1200",
"discount": "$12"
},
{
"date": "2017-12-31T06:12:04Z",
"order_id": 1000,
"amount": "$1200",
"discount": "$12"
}
]
},
"schema": {
"transactions": {
"primary_key": [
"order_id",
"date"
]
}
},
"hasMore": "false"
}
For more information about the fields in the response, see our response format documentation.
Real-world sample function
Download our sample Transport - API (London Tube) function. Select your language to download the sample function (.zip file):
Create a function using the downloaded .zip file. For more information, see Deploying from Cloud Console.