OnePost

FREEMIUM
By Adam Darrah | Updated a month ago | Social
Popularity

8.5 / 10

Latency

954ms

Service Level

100%

Health Check

N/A

Back to All Tutorials (10)

The Webhook System

If you’ve been following the previous tutorials, you may have noticed that in some cases records are created within OnePost automatically. You can use Get requests to find these records. However, the OnePost Webhook system exists to solve this problem by letting your application know when key events happen using an HTTP Post method.

In this tutorial, we’ll walk through an example of how you might use this in the context of testing. Our first requirement is that we need an HTTP endpoint on the internet that is capable of accepting POST requests. In practice, this would be an endpoint hosted by your application and setup for processing the incoming data. However, for this tutorial, we’ll simply use the Webhook.site service to test. Simply click on the link to get a unique URL.

Once you have your unique URL, let’s use the Create a Webhook API endpoint to add a new Webhook record.

curl --request POST --url 'https://onepost1.p.rapidapi.com/api/v1/webhooks?secret_key=sk-cbe...' --header 'content-type: application/json' --header 'x-rapidapi-host: onepost1.p.rapidapi.com' --header 'x-rapidapi-key: 9d0...' --data '{   
    "webhook": {
        "endpoint_url": "https://webhook.site/3f338283-10b0-492a-a97e-d891d538ab79"
    }
}'

{"id":5,"endpoint_url":"https://webhook.site/3f338283-10b0-492a-a97e-d891d538ab79","created_at":"2021-02-22T11:32:04.571-05:00","updated_at":"2021-02-22T11:32:04.571-05:00"}

Now that we registered our endpoint, it will receive “event” requests when events are automatically created within OnePost. However, we can test to make sure we’ve set things up correctly using the Create a Test Event API Endpoint.

curl --request POST \
> --url 'https://onepost1.p.rapidapi.com/api/v1/events/test?secret_key=sk-cbe...' \
> --header 'content-type: application/json' \
> --header 'x-rapidapi-host: onepost1.p.rapidapi.com' \
> --header 'x-rapidapi-key: 9d0...' \
> --data '{}'

{"id":31,"name":"events.test","data":{"object":{"ping":"pong"}},"created_at":"2021-02-22T11:34:43.773-05:00","updated_at":"2021-02-22T11:34:43.773-05:00","webhook_attempts":[{"id":26,"state":"new","created_at":"2021-02-22T11:34:43.808-05:00","updated_at":"2021-02-22T11:34:43.808-05:00","number_of_attempted_requests":0,"webhook":{"id":5,"endpoint_url":"https://webhook.site/3f338283-10b0-492a-a97e-d891d538ab79","created_at":"2021-02-22T11:32:04.571-05:00","updated_at":"2021-02-22T11:32:04.571-05:00"}}]}

Once this request is successful, you can see that it was received by Webhook.site:

OnePost currently only has a handful of event types, but check back later for more.

  1. authorized_page.authorized: Created automatically when a new AuthorizedPage is “touched” (after a user signs in to the provider to allow access). Then, created automatically daily to refresh information about the AuthorizedPage (such as name, profile image, etc).
  2. events.test: Created manually by the API user to test Webhook endpoints are configured correctly
  3. post.published: Created automatically when a Post has successfully posted to all AuthorizedPages (i.e. All SocialPosts have a state of “sent”)
  4. social_post.retrying: Created automatically if the SocialPost fails to publish for any reason. Onepost will continue to attempt to publish the SocialPost after a failure. Listen for the post.published or social_post.updated events to indicate an eventual success.
  5. social_post.updated: Created automatically for each SocialPost after a Post has been published. Listen for this event if you want to track performance (such as the number of likes, retweets, etc).