Search code examples
javascriptnode.jstwiliotwilio-functions

Memory object in twilio


I need to maintain a common variable among 2 functions in Twilio. But it's not working as expected. I tried to use variable inside memory like this:-

    let memory = JSON.parse(event.Memory);
    if(memory.twilio.counter === null) {
        memory.twilio.counter = 0;
    } else {
        memory.twilio.counter = memory.twilio.counter + 1;
    }

Is it not the correct way? If not, is there any alternative?


Solution

  • Memory is a object presented by Twilio Autopilot, not Twilio Functions. If you want to share state between Functions (not using Autopilot), you need to place that state into external storage like Twilio Sync or Airtable, etc.

    Sync is good is there are not a lot of read/write operations. See Tips for building with Sync API below.

    Use Twilio Sync to create, read, update, delete persistent data

    Tips for building with Sync API