> For the complete documentation index, see [llms.txt](https://docs.iotify.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.iotify.io/additional-helpers/iotify-helpers/messaging-functions.md).

# Messaging Functions

### HTTP REST APIs

Within your device template, you could call a REST API and get data from an external source, or push data to an external service.&#x20;

#### GET Requests

```javascript
//requires the resource URL as the argument
rest.get({url: ''})  

rest.get({url:'https://httpbin.org/get'})

//you can also create a separate object for the argument and pass it to the API 
//headers are also supported
let options = {
	url: "https:https://httpbin.org/get",
	headers: {
		'content-type': 'application/json'
	}
}

rest.get(options)  
```

#### POST Requests

```javascript
//requires the resource URL and a message object as the arguments
rest.post({url: '', json: {}})

rest.post({url:'https://httpbin.org/post', json: { hello: 'world'}})

//you can also create a separate object for the arguments and pass it to the API  
let obj = {
	username: "test_user",
	password: "dontusethis"
}

//headers are also supported
let options = {
	url: "https://httpbin.org/post",
	json: obj,
	headers: {
		'content-type': 'application/json'
	}
}

rest.post(options);
```

#### PUT Requests

```javascript
//requires the resource URL and a message object as the arguments
rest.put({url: '', json: {}})

rest.put({url:'https://httpbin.org/put', json: { hello: 'world'}})

//you can also create a separate object for the arguments and pass it to the API  
let options = {
	url: "https://httpbin.org/put",
	json: { hello: 'world'},
	headers: {
		'content-type': 'application/json'
	}
}

rest.put(options);
```

#### PATCH Requests

```javascript
//requires the resource URL and a message object as the arguments
rest.patch({url: '', json: {}})

rest.patch({url:'https://httpbin.org/patch', json: { hello: 'world'}})

//you can also create a separate object for the arguments and pass it to the API  
let options = {
	url: "https://httpbin.org/patch",
	json: { hello: 'world'},
}

rest.patch(options);
```

#### DELETE Requests

```javascript
//requires the resource URL the argument
rest.delete({url: ''})

rest.delete({url:'https://httpbin.org/delete'})

//you can also create a separate object for the arguments and pass it to the API  
let options = {
	url: "https://httpbin.org/delete",
	headers: {
		'content-type': 'application/json'
	}
}

rest.delete(options);
```

### MQTT APIs

Within the device template, you can use the MQTT APIs to interact with the MQTT connections.

#### Publish

To publish a message to an MQTT endpoint, you can use the following function.

```javascript
//requires a payload object and a topic string as arguments
mqtt.publish(payload, topic)

let payload = {
    hello: 'world'
}

let topic = "testTopic"

mqtt.publish(payload, topic)
```

#### Resubscribe

To resubscribe to multiple topics use the following function.

```javascript
//requires an array of topics as the argument
mqtt.resubscribe([topics])

mqtt.resubscribe([topic1, topic2, topic3])
```

#### Force Disconnect

To disconnect from all MQTT topics and close all connections, use the following function.&#x20;

```javascript
//no arguments are required for this function
mqtt.forceDisconect()
```

#### Force Connect

To connect to all previously connected MQTT connections, use the following function.

```javascript
//no arguments are required for this function
mqtt.forceConect()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.iotify.io/additional-helpers/iotify-helpers/messaging-functions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
