Search code examples
resttwiliomessaging

Twilio IP Messaging user not found


I'm trying to add a users identity to a channel using the REST API using instructions here: https://www.twilio.com/docs/api/ip-messaging/rest/members#action-create

I'm posting to the /Channels/channelId/Members endpoint - I'm certain my request is structured correctly.

I get an error back from Twilio IP Messaging saying:

{"code": 50200, "message": "User not found", "more_info": "https://www.twilio.com/docs/errors/50200", "status": 400}

My understanding was that we can provide our own identity when we want to add someone to a Channel. How can I 'register' the user (with an email) before adding them to the Channel?

EDIT - The code:

var _getRequestBaseUrl = function() {
  return 'https://' +
    process.env.TWILIO_ACCOUNT_SID + ':' +
    process.env.TWILIO_AUTH_TOKEN + '@' +
    TWILIO_BASE + 'Services/' +
    process.env.TWILIO_IPM_SERVICE_SID + '/';
};

var addMemberToChannel = function(memberIdentity, channelId) {                                          
  var options = {
    url: _getRequestBaseUrl() + 'Channels/' + channelId + '/Members',                              
    method: 'POST',                                                                                
    headers: {
      'content-type': 'application/x-www-form-urlencoded',                                         
    },
    form: {
      Identity: memberIdentity,                                                                    
    },
  };                                                                                           
  request(options, function(error, response, body) {
    if (error) {
       // Getting the error here
    }
    // do stuff with response.
  });
};                                                             
addMemberToChannel('test1@example.com', <validChannelId>);

Solution

  • Twilio developer evangelist here.

    In order to add a user to be a member of a channel, you do indeed need to register them first. Check out the documentation for creating a user in IP Messaging.

    With your code you'd need a function like:

    var createUser = function(memberIdentity) {
      var options = {
        url: _getRequestBaseUrl() + 'Users',
        method:'POST',
        headers: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        form: {
          Identity: memberIdentity,            
        }
      };
    
      request(options, function(error, response, body) {
        if (error) {
           // User couldn't be created
        }
        // do stuff with user.
      });
    }
    

    Could I also suggest you take a look at the Twilio helper library for Node.js. It handles the creation of URLs like you're doing for you. The code looks cleaner too, you can create a user with the helper library like this:

    var accountSid = 'ACCOUNT_SID';
    var authToken = 'AUTH_TOKEN';
    var IpMessagingClient = require('twilio').IpMessagingClient;
    
    var client = new IpMessagingClient(accountSid, authToken);
    var service = client.services('SERVICE_SID');
    
    service.users.create({
        identity: 'IDENTITY'
    }).then(function(response) {
        console.log(response);
    }).fail(function(error) {
        console.log(error);
    });
    

    Let me know if this helps at all.