Adafruit IO REST API & MQTT API with Swift (iOS)
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit IO REST API & MQTT API with Swift (iOS)

by GusBergamotte on Sat Feb 16, 2019 3:16 pm

Here are a few tips regarding using the Adafruit IO REST API and the MQTT API with Swift 4.x to build a Mac/iOS application:


- Follow the instructions at https://github.com/adafruit/io-api to "generate client libraries with Swagger" - select the Swift language.

- The Swift libraries generated will require some tweaking - I am still impressed that about 90% of the generated Swift libraries can be used as is.

- Apart from a few syntax issues, another problem that I found was that the "Any" type that is used in some of the structs (such as "Activity") cannot be used with the Codable (i.e. Decodable & Encodable) protocols. However, this is quite easy to fix. You can replace the "Any" type by "AnyCodable". AnyCodable was developed by "Matt" and can be downloaded at https://github.com/Flight-School/AnyCodable.

- All the REST API calls that I tried worked really well - here is just a sample on how you can make such a call in Swift:

/* Note that you have to replace ioKey by your own Adafruit IO Key */

SwaggerClientAPI.customHeaders = ["X-AIO-Key" : ioKey]

// Get information about the current user.

UsersAPI.currentUser { (user, error) in
if let user = user {

if let error = error {


- There is a great MQTT client that is available here: https://github.com/novastone-media/MQTT-Client-Framework. The library is in Objective C but it can be easily accessed in Swift.

- I was able to publish and subscribe to my AIO feeds and receive JSON data from a subscription such as the following (note the sample output has been changed to remove personal information):

Received 196 bytes on:AIOUserName/feeds/digital/json qMQTTQosLevel rfalse m0
"updated_at" : "2019-02-15 21:37:32 UTC",
"data" : {
"location" : null,
"created_at" : "2019-02-15T21:37:32.628Z",
"value" : "1",
"id" : "0"
"id" : 0,
"last_value" : "1",
"key" : "digital"

Hopefully, this information can be useful to someone.



Posts: 1
Joined: Thu Feb 14, 2019 3:29 pm

Please be positive and constructive with your questions and comments.