Integrate With Your App

Overview

Now that your HostedHooks instance has been setup (previous step), we are now ready to start integrating HostedHooks into your App.

1. Pick your programming language

The HostedHooks API can be used easily with most programming languages. We will add code samples for several of them in this guide.
  • Ruby
  • PHP
  • Javascript
  • Laravel

2. Set environment variables

The HostedHooks API key is required for all API requests. It should be kept secret and set as an environment variable.
1
HOSTEDHOOKS_API_KEY='VdaYqFdqwrtqmQr6tJTuEBF1'
Copied!

3. Copy your App identifier

Before we jump into the code we will need to grab your App identifier (ID) from the HostedHooks dashboard. Once at your dashboard, click View on your App and then click the copy icon to grab the unique ID of your app object.

3. Send a webhook message

In this step we will walk through how you trigger sending webhooks from your app.
The endpoint to send messages is documented here. The URI for that endpoint is below. It has one parameter, the app_id which you copied from the dashboard in the previous step.
1
"https://www.hostedhooks.com/api/v1/apps/:app_id/messages"
Copied!
In addition to the app_id, the event_type is used to determine which subscribers will receive your webhook messages.
If you need to send webhooks to a specific subscriber's endpoint, you can use the API endpoint documented here.
The data object within the message payload is an open JSON object that allows you to pass in any information that you want. The data inside this payload will be passed on to your subscribers via the webhook endpoint they setup.
Ruby
PHP
Laravel
Javascript
1
def send_webhook_message()
2
uri = URI("https://www.hostedhooks.com/api/v1/apps/:app_uuid/messages")
3
4
headers = {"Content-Type": "application/json", "Authorization": "Bearer #{ENV['HOSTEDHOOKS_API_KEY']}"}
5
6
# Build message payload
7
message_payload = {
8
data: {
9
user: {
10
id: "1337",
11
notes: "foobar"
12
}
13
},
14
version: "1.0",
15
event_type: "user.created"
16
}
17
}
18
19
# Create Request
20
http = Net::HTTP.new(uri.host, uri.port)
21
http.use_ssl = true
22
request = Net::HTTP::Post.new(uri.request_uri, headers)
23
request.body = message_payload.to_json
24
25
# Send the request
26
response = http.request(request)
27
28
puts "Response HTTP Status Code: #{ response.code }"
29
puts "Response HTTP Response Body: #{ response.body }"
30
rescue StandardError => e
31
puts "HTTP Request failed (#{ e.message })"
32
end
Copied!
1
function sendWebhookMessage()
2
{
3
$apiKey = "your-api-key";
4
5
$uri = "https://www.hostedhooks.com/api/v1/apps/{app_uuid}/messages";
6
7
/* Build message payload*/
8
$messagePayload = [
9
'data' => [
10
'user' => [
11
'id' => '1337',
12
'note' => 'foobar'
13
],
14
],
15
'version' => '1.0',
16
'event_type' => 'user.created'
17
];
18
19
$headers = array(
20
'Content-Type: application/json',
21
'Authorization: Bearer ' . $apiKey,
22
);
23
24
$ch = curl_init($uri);
25
26
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
27
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
28
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
29
30
curl_setopt($ch, CURLOPT_POST, true);
31
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messagePayload));
32
33
$response = curl_exec($ch);
34
35
if ($response === false) {
36
return [
37
'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
38
'error' => "CURL Error: " . curl_error($ch),
39
];
40
}
41
42
return [
43
'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
44
'body' => json_decode($response, true),
45
];
46
}
Copied!
1
use Illuminate\Support\Facades\Http;
2
use Illuminate\Http\Client\RequestException;
3
4
class HostedHooksService
5
{
6
public function sendWebhookMessage()
7
{
8
$uri = "https://www.hostedhooks.com/api/v1/apps/{app_uuid}/messages";
9
10
/* Build message payload*/
11
$messagePayload = [
12
'data' => [
13
'user' => [
14
'id' => '1337',
15
'notes' => 'foobar',
16
],
17
],
18
'version' => '1.0',
19
'event_type' => 'user.created',
20
];
21
22
try {
23
$response = Http::withToken(env('HOSTEDHOOKS_API_KEY'))
24
->post($uri, $messagePayload);
25
26
//Throw exception if a client or server error occurred
27
$response->throw();
28
29
//Otherwise go ahead with successful response
30
Log::info('Request sent successfully', [
31
'Status code' => $response->status(),
32
'Response body' => $response->body()
33
]);
34
35
} catch (RequestException $e) {
36
Log::error('HTTP Request Failed', ['message' => $e->getMessage()]);
37
}
38
}
39
}
Copied!
1
function sendMessage() {
2
var url = new URL("https://www.hostedhooks.com/api/v1/apps/${app_uuid}/messages");
3
4
var myHeaders = new Headers();
5
myHeaders.append("Authorization", "Bearer HOSTEDHOOKS_API_KEY");
6
myHeaders.append("Content-Type", "application/json");
7
8
/* Build message payload */
9
var messagePayload = JSON.stringify({
10
"data": {
11
"user": {
12
"id": "1337",
13
"notes": "foobar",
14
}
15
},
16
"version": "1.0",
17
"event_type": "user.created"
18
});
19
20
var requestOptions = {
21
method: 'POST',
22
headers: myHeaders,
23
body: messagePayload,
24
redirect: 'follow'
25
};
26
27
fetch(url, requestOptions)
28
.then(response => response.text())
29
.then(result => console.log(result))
30
.catch(error => console.log('error', error));
31
}
Copied!
On a successful Message API request, you will receive back a 200 and a payload like the one below.
1
{
2
"id": "4e4fffcb-295b-4b79-cda3-02cc4720f0ba",
3
"data": {
4
"user": {
5
"id": '1337',
6
"notes": "foobar"
7
}
8
},
9
"event_id": "07a1cf3fd7cced67a7fd",
10
"event_type": "user.created",
11
"created_at": "2021-05-11T08:51:40.679-04:00",
12
"app": {
13
"id": "bf111747-9635-46fb-as23-9b6e45402f47",
14
"name": "SaaS Site",
15
"created_at": "2021-03-31T21:24:48.376-04:00"
16
}
17
}
Copied!
Once you've received a successful response, you've successfully triggered a webhook call from your app. We will now review the subscriber side to receive the webhook you sent.
Last modified 4mo ago