Chat SDK
Embed chat in your app with client SDKs for iOS, Android, and JavaScript.
  • Send a message

    Twilio will send the message to all the members of a channel. If a member is offline, the message is queued for delivery.

    View Docs
    • JavaScript
    • Objective-C
    • Swift
    • Android
    channel.sendMessage("Draw the owl!")
    NSString *message = @"Draw the owl!";
    [self.channel.messages sendMessage:message completion:^(TWMResult result) {
      if (result != TWMResultFailure) {
        NSLog(@"message sent...%@", result);
      }
    }];
    let message: TMMessage = channel.messages.createMessageWithBody("Draw the owl!")
    channel.messages.sendMessage(message) { result in
      if result == .Success {
        print("Message sent %@", result)
      }
    }
    Message message = messagesObject.createMessage("Draw the owl!");
    
    messagesObject.sendMessage(message, new StatusListener() {
      @Override
      public void onSuccess() {
        logger.e("Successful at sending message.");
      }
    });
  • Receive a message

    Channels receive events when new messages are added. Process the events client-side or query for a specific message.

    View Docs
    • JavaScript
    • Objective-C
    • Swift
    • Android
    // Callback
    channel.on('messageAdded', function(message) {
      console.log(message.author, message.body);
    });
    
    // Promise
    channel.on('messageAdded')
      .then(function(message) {
    	  console.log(message.author, message.body);
    });
    -(void)ipMessagingClient:(TwilioIPMessagingClient *)client channel:(TWMChannel *)channel messageAdded:(TWMMessage *)message {
      [self addMessages:@[message]];
    }
    extension ViewController: TwilioIPMessagingClientDelegate {
    
      // Called whenever a channel we've joined receives a new message
      func ipMessagingClient(client: TwilioIPMessagingClient!, channel: TMChannel!,
          messageAdded message: TMMessage!) {
        print("\(message.author) said: \(message.body)")
      }
    }
    @Override
    public void onMessageAdd(Message message) {
      System.out.println("message added");
    }
  • Get chat history

    Channel history is stored in the cloud and synced across devices.

    View Docs
    • JavaScript
    • Objective-C
    • Swift
    • Android
    channel.getMessages(25)
      .then(function(messages) {
        for (i=0; i<messages.length; i++) {
          var message = messages[i];
          console.log('Message: ' + message.body);
        }
    });
    NSArray *messages = self.channel.messages.allObjects;
    NSLog(@"joined general channel with the following messages: %@", messages);
    for (id msg in messages) {
      NSLog(@"Message:%@", msg);
    }
    let allMessages = channel.messages.allObjects()
    for message in allMessages {
      print("message body: \(message.body)")
    }
    Messages messagesObject = channel.getMessages();
    
    Message[] messagesArray = messagesObject.getMessages();
    for(int x = 0; x< messagesArray.length; x++) {
      System.out.println("Message body:" + messagesArray[x]);
    }
  • Subscribe to typing indicator

    Typing indicators show when someone is typing. Create a handler to process the event.

    View Docs
    • JavaScript
    • Objective-C
    • Swift
    • Android
    // Set up the listener for the typing started Channel event
    activeChannel.on('typingStarted', function(member) {
    
      // Process the member to show typing
      console.log("typing started for " + member);
    });
    -(void)ipMessagingClient:(TwilioIPMessagingClient *)client typingStartedOnChannel:(TWMChannel *)channel member:(TWMMember *)member {
      NSLog(@"typing started for %@", member);
    }
    func ipMessagingClient:(TwilioIPMessagingClient!, channel: TMChannel!, member: TMMember  {
      NSLog(@"typing started for %@", member);
    }
    public void onTypingStarted(Member member){
      System.out.println("typing started for " +  member);
    }
Chat API
Add backend logic to your chat server with REST APIs and helper libraries.
  • Send a message

    Send a message to any channel from any user directly from your backend.

    View Docs
    • C#
    • Java
    • Node
    • PHP
    • Python
    • Ruby
    • Curl
    var client = new TwilioIpMessagingClient(accountSid, authToken);
    Message message = client.CreateMessage(serviceSid, channelSid, memberSid, body);
    Console.WriteLine(message);
    final Map<String, String> messageParams = new HashMap<String, String>();
    messageParams.put("Body", "MESSAGE");
    Message message = channel.getMessages().create(messageParams);
    System.out.println(message);
    service.channels('CHANNEL_ID').messages('MESSAGE_ID').create({
      body: 'MESSAGE'
    }).then(function(response) {
      console.log(response);
    }).fail(function(error) {
      console.log(error);
    });
    $message = $channel->messages->create(array(
      "body" => "MESSAGE",
    ));
    print $message
    messages = channel.messages.create(body="MESSAGE")
    print message
    service = ip_messaging_client.services.get('SERVICE_SID')
    channel = service.channels.get('CHANNEL_SID')
    response = channel.messages.create(body:'MESSAGE')
    puts response
    curl -XPOST https://chat.twilio.com/v1/Services/{service sid}/Channels/{Channel SID}/Messages \
      -d "Body=Jenny, Jenny, who can I turn to?" \
      -u '{twilio account sid}:{twilio auth token}'
  • Filter Keywords

    Receive webhooks for each message, which allows you to parse keywords and execute intelligent logic.

    View Docs
    • C#
    • Java
    • Node
    • PHP
    • Python
    • Ruby
    • Curl
    [HttpPost]
    public ActionResult IpMsgWebhook(string from, string to, string body, string trigger)
    {
      if(trigger == "onMessageSend")
      {
        return WebBot.ProcessMessage(from, to, body);
      }
      return new HttpStatusCodeResult(200);
    }
    @WebServlet("/ip-msg-webhook")
    public class IpmServlet extends HttpServlet {
      public void doGet(HttpServletRequest request, HttpServletResponse response)
          throws IOException {
        if (request.getParameter("trigger") == "onMessageSend") {
          String message = request.getParameter("message");
          String to = request.getParameter("to");
          String from = request.getParameter("from");
          processMessage(message, to, from);
        }
        response.getWriter().println("received message");
      }
    }
    app.post('/ip-msg-webhook', function(request, response) {
      var trigger = request.query.trigger;
    
      if (trigger == 'onMessageSend') {
        sendToWebbot(request, response);
      } else {
        response.sendStatus(200);
      }
    });
    if ($_GET['trigger'] == 'onMessageSend') {
      processMessage($_GET['message'], $_GET['to'], $_GET['from']);
    }
    echo "received message";
    from flask import Flask, request
    app = Flask(__name__)
    
    @app.route("/ip-msg-webhook")
    def ipm_webhook():
        if request.args.get("trigger") == "onMessageSend"
            message = request.args.get("message")
            to = request.args.get("to")
            from_ = request.args.get("from")
            process_message(message, to, from_)
    
        return "received message"
    
    if __name__ == "__main__":
        app.run()
    require 'sinatra'
    
    get '/ip-msg-webhook' do
      if params[:trigger] == 'onMessageSend'
        process_message params[:message], params[:to], params[:from]
      else
        "received message"
    end
    # Nothing to see, here's an owl instead.
    
    	              , _ ,
    	             ( o o )
    	            /'` ' `'\
    	            |'''''''|
    	            |\\'''//|
    	               """
  • Add channel members

    Programmatically assign users to a channel. To enable support use cases, you can put a customer and agent in the same channel.

    View Docs
    • C#
    • Java
    • Node
    • PHP
    • Python
    • Ruby
    • Curl
    var client = new TwilioIpMessagingClient(accountSid, authToken);
    Member member = client.CreateMember(credentialSid, channelSid, identity, roleSid);
    Console.WriteLine(member);
    final Map<String, String> memberParams = new HashMap<String, String>();
    memberParams.put("Identity", "IDENTITY");
    Member member = Channel.getMembers().create(memberParams);
    System.out.println(member);
    service.channels('CHANNEL_SID').members.create({
      identity: "IDENTITY"
    }).then(function(response) {
      console.log(response);
    }).fail(function(error) {
      console.log(error);
    });
    $member = $channel->members.create(array(
      "identity" => "IDENTITY",
    ));
    print $member;
    service = client.services.get(sid="SERVICE_SID")
    channel = service.channels.get(sid="CHANNEL_SID")
    member = channel.members.create(identity="IDENTITY")
    print member
    service = ip_messaging_client.services.get('SERVICE_SID')
    channel = service.channels.get('CHANNEL_SID')
    member = channel.members.create(Identity:'IDENTITY')
    puts member
    curl -XPOST https://chat.twilio.com/v1/Services/{service sid}/Channels/{Channel SID}/Members \
      -d "Identity=kwhinnery" \
      -u '{twilio account sid}:{twilio auth token}'
The Twilio advantage
  • Communicate reliably

    Experience a 99.95% uptime SLA made possible with automated failover and zero-maintenance windows.

  • Operate at scale

    Extend the same app you write once to new markets with configurable features for localization and compliance.

  • Many channels

    Use the same platform you know for voice, SMS, video, chat, two-factor authentication, and more.

  • No shenanigans

    Get to market faster with pay-as-you-go pricing, free support, and the freedom to scale up or down without contracts.