How to Send a WhatsApp API using Swift

Introduction

In this code snippet, you learn how to send a WhatsApp API using Swift, to send a text message, WhatsApp Templates, media, interactive buttons, lists, products … etc, Alvochat account is required to run the following codes. Create an Account if you don’t have one.

Send template using WhatsApp API & Swift

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&name=hello_world".data(using: String.Encoding.utf8)!)
postData.append("&language=en_us".data(using: String.Encoding.utf8)!)
postData.append("&header=".data(using: String.Encoding.utf8)!)
postData.append("&body=".data(using: String.Encoding.utf8)!)
postData.append("&buttons=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/template")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send a text message using WhatsApp API & Swift

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&body=WhatsApp API on alvochat.com works good".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&preview_url=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/chat")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send image

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&image=https://alvochat-example.s3-accelerate.amazonaws.com/image/1.jpeg".data(using: String.Encoding.utf8)!)
postData.append("&caption=image caption".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/image")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send audio

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&audio=https://alvochat-example.s3-accelerate.amazonaws.com/audio/1.mp3".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/audio")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send video

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&video=https://alvochat-example.s3-accelerate.amazonaws.com/video/1.mp4".data(using: String.Encoding.utf8)!)
postData.append("&caption=video caption".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/video")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send document

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&document=https://alvochat-example.s3-accelerate.amazonaws.com/document/1.pdf".data(using: String.Encoding.utf8)!)
postData.append("&filename=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/document")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send sticker

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&sticker=https://alvochat-example.s3-accelerate.amazonaws.com/sticker/1.webp".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/sticker")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send Contact

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&contact=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/contact")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send Location

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&lat=37.484296".data(using: String.Encoding.utf8)!)
postData.append("&lng=-122.148703".data(using: String.Encoding.utf8)!)
postData.append("&address=Menlo Park, California, United States".data(using: String.Encoding.utf8)!)
postData.append("&name=Meta Headquarters".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/location")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send list

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&header=header".data(using: String.Encoding.utf8)!)
postData.append("&body= please select one of the following options".data(using: String.Encoding.utf8)!)
postData.append("&footer=footer".data(using: String.Encoding.utf8)!)
postData.append("&button=options".data(using: String.Encoding.utf8)!)
postData.append("§ions=option_1,option_2,option_3".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/list")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send button

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&header=header".data(using: String.Encoding.utf8)!)
postData.append("&body= please select one of the following options".data(using: String.Encoding.utf8)!)
postData.append("&footer=footer".data(using: String.Encoding.utf8)!)
postData.append("&buttons=option_1,option_2,option_3".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/button")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

Send products

import Foundation
import Cocoa

let postData = NSMutableData(data: "send=true".data(using: String.Encoding.utf8)!)
postData.append("&token=YourToken".data(using: String.Encoding.utf8)!)
postData.append("&to=16315555555".data(using: String.Encoding.utf8)!)
postData.append("&header=header".data(using: String.Encoding.utf8)!)
postData.append("&body=Hi , check out our new products".data(using: String.Encoding.utf8)!)
postData.append("&footer=footer".data(using: String.Encoding.utf8)!)
postData.append("&catalog_id=".data(using: String.Encoding.utf8)!)
postData.append("&product=".data(using: String.Encoding.utf8)!)
postData.append("&priority=".data(using: String.Encoding.utf8)!)
postData.append("&message_id=".data(using: String.Encoding.utf8)!)


let request = NSMutableURLRequest(url: NSURL(string: "https://api.alvochat.com/instance1199/messages/product")! as URL,cachePolicy: .useProtocolCachePolicy,timeoutInterval: 10.0)
request.httpMethod = "POST"
request.httpBody = postData as Data

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
  if (error != nil) {
    print(error)
  } else {
    let httpResponse = response as? HTTPURLResponse
    print(httpResponse)
  }
})

dataTask.resume()

You can see the Full Whatsapp API Documentation here.