Azure Functions - Sample Functions
Code samples to help you build your Azure Function. The pieces of code show what an Azure 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
JavaScript
javascript
module.exports = function(context, req) {
if (req.body.state === undefined) {
context.res = {
status: 400,
body: "No state is defined!"
};
}
if (req.body.secrets === undefined) {
context.res = {
status: 400,
body: "No secret is defined!"
};
}
context.res = {
status: 200,
body: update(req.body.state, req.body.secrets),
headers: {
"Content-Type": "application/json"
}
};
context.done();
};
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):
Use Azure Functions's zip push deployment to deploy the function. For more information, see Zip deployment for Azure Functions.