Search code examples
twiliotwilio-conversations

returning empty object Nodejs adding attributes to twilio conversations API


I am using twilio conversations Api to create a little sms chat app. I need to set an attributes object , but when I set it like below it just returns empty. Any twilio experts out there know how to solve this?

NOTE from docs :

Attributes -

An optional string metadata field you can use to store any data you wish. The string value must contain structurally valid JSON if specified. Note that if the attributes are not set "{}" will be returned.

  await subClient.conversations.services(convoServiceSid)
        .conversations
        .create({
            attributes: {read: "false"}  // when I fetch conversation, this attributes is empty object....  
        })
        .then(conversation => console.log(conversation.sid));

Solution

  • So, in the docs it says The string value must contain STRUCTURALLY VALID JSON

    the Solution is as follows

     await subClient.conversations.services(convoServiceSid)
            .conversations
            .create({
                attributes: '{"read": "false"}'  <-- needed to wrap object in quotes, also need to wrap key in quotes! success! 
            })
            .then(conversation => console.log(conversation.sid));