### Webhooks API Subscription Examples (HTTP, .NET, Ruby, PHP, Python) Source: https://developers.mindbodyonline.com/ui/documentation/webhooks-api Examples demonstrating how to interact with the Mindbody Webhooks API for subscription management. These snippets illustrate common operations like creating, activating, and deactivating webhook subscriptions using various programming languages and the HTTP protocol. They are designed to work with any language that has an HTTP library. ```http POST /webhooks/v1/subscriptions Host: api.mindbody.com Content-Type: application/json Authorization: Bearer YOUR_API_KEY { "callbackUrl": "https://your-webhook-url.com/", "eventFilters": [ "Client.Added", "Client.Updated" ] } ``` ```csharp using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; public class WebhookClient { private readonly HttpClient _httpClient; private readonly string _apiKey; public WebhookClient(string apiKey) { _httpClient = new HttpClient { BaseAddress = new Uri("https://api.mindbody.com/webhooks/v1/") }; _apiKey = apiKey; _httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {_apiKey}"); } public async Task CreateSubscriptionAsync(string callbackUrl, string[] eventFilters) { var payload = new { callbackUrl = callbackUrl, eventFilters = eventFilters }; var jsonPayload = System.Newtonsoft.Json.JsonConvert.SerializeObject(payload); var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); var response = await _httpClient.PostAsync("subscriptions", content); response.EnsureSuccessStatusCode(); Console.WriteLine("Subscription created successfully."); } public async Task ActivateSubscriptionAsync(string subscriptionId) { var response = await _httpClient.SendAsync(new HttpRequestMessage { Method = new HttpMethod("PATCH"), RequestUri = new Uri($"subscriptions/{subscriptionId}/activate"), Content = new StringContent("{}", Encoding.UTF8, "application/json") // Empty JSON body for activation }); response.EnsureSuccessStatusCode(); Console.WriteLine($"Subscription {subscriptionId} activated."); } public async Task DeactivateSubscriptionAsync(string subscriptionId) { var response = await _httpClient.DeleteAsync($"subscriptions/{subscriptionId}"); response.EnsureSuccessStatusCode(); Console.WriteLine($"Subscription {subscriptionId} deactivated."); } } ``` ```ruby require 'net/http' require 'uri' require 'json' class MindbodyWebhook def initialize(api_key) @api_key = api_key @base_uri = URI('https://api.mindbody.com/webhooks/v1/') end def create_subscription(callback_url, event_filters) http = Net::HTTP.new(@base_uri.host, @base_uri.port) http.use_ssl = true request = Net::HTTP::Post.new(@base_uri) request['Content-Type'] = 'application/json' request['Authorization'] = "Bearer #{@api_key}" payload = { callbackUrl: callback_url, eventFilters: event_filters }.to_json request.body = payload response = http.request(request) raise "Failed to create subscription: #{response.body}" unless response.is_a?(Net::HTTPSuccess) JSON.parse(response.body) end def activate_subscription(subscription_id) uri = URI.join(@base_uri, "subscriptions/#{subscription_id}/activate") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Patch.new(uri) request['Authorization'] = "Bearer #{@api_key}" request['Content-Type'] = 'application/json' request.body = '{}' # Empty JSON body for activation response = http.request(request) raise "Failed to activate subscription #{subscription_id}: #{response.body}" unless response.is_a?(Net::HTTPSuccess) true end def deactivate_subscription(subscription_id) uri = URI.join(@base_uri, "subscriptions/#{subscription_id}") http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true request = Net::HTTP::Delete.new(uri) request['Authorization'] = "Bearer #{@api_key}" response = http.request(request) raise "Failed to deactivate subscription #{subscription_id}: #{response.body}" unless response.is_a?(Net::HTTPSuccess) true end end ``` ```php apiKey = $apiKey; } public function createSubscription($callbackUrl, $eventFilters) { $url = $this->baseUrl . 'subscriptions'; $data = [ 'callbackUrl' => $callbackUrl, 'eventFilters' => $eventFilters ]; $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . $this->apiKey ]; return $this->sendRequest('POST', $url, $data, $headers); } public function activateSubscription($subscriptionId) { $url = $this->baseUrl . 'subscriptions/' . $subscriptionId . '/activate'; $headers = [ 'Content-Type: application/json', 'Authorization: Bearer ' . $this->apiKey ]; // Activation typically uses an empty JSON body return $this->sendRequest('PATCH', $url, '{}', $headers); } public function deactivateSubscription($subscriptionId) { $url = $this->baseUrl . 'subscriptions/' . $subscriptionId; $headers = [ 'Authorization: Bearer ' . $this->apiKey ]; return $this->sendRequest('DELETE', $url, null, $headers); } private function sendRequest($method, $url, $data = null, $headers = []) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); if ($data !== null && $method !== 'DELETE') { curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? json_encode($data) : $data); } $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode >= 200 && $httpCode < 300) { return json_decode($response, true); } else { throw new Exception("Request failed with HTTP code {$httpCode}: {$response}"); } } } ?> ``` ```python import requests import json class MindbodyWebhook: def __init__(self, api_key): self.api_key = api_key self.base_url = "https://api.mindbody.com/webhooks/v1/" def create_subscription(self, callback_url, event_filters): url = self.base_url + "subscriptions" payload = { "callbackUrl": callback_url, "eventFilters": event_filters } headers = { "Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}" } response = requests.post(url, headers=headers, data=json.dumps(payload)) response.raise_for_status() return response.json() def activate_subscription(self, subscription_id): url = f"{self.base_url}subscriptions/{subscription_id}/activate" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}" } # Activation typically uses an empty JSON body response = requests.patch(url, headers=headers, data='{}') response.raise_for_status() return response.json() def deactivate_subscription(self, subscription_id): url = f"{self.base_url}subscriptions/{subscription_id}" headers = { "Authorization": f"Bearer {self.api_key}" } response = requests.delete(url, headers=headers) response.raise_for_status() return response.json() ``` -------------------------------- ### Mindbody Webhooks API Overview Source: https://developers.mindbodyonline.com/ui/documentation/webhooks-api General information about the Mindbody Webhooks API, including its purpose, communication protocol, and data format. ```APIDOC ## Mindbody Webhooks API ### Description The Mindbody Webhooks API notifies you when specific events are triggered by a business that uses Mindbody. You can use this API to create an application that shows near real-time updates to a business's data without having to long-poll the Public API. This API is Mindbody's implementation of an HTTP push API. ### Protocol and Data Format The Webhooks API uses the HTTP protocol, so it works with any language that has an HTTP library. Requests use standard HTTP verbs such as `GET`, `POST`, and `DELETE`. All endpoints accept and return JSON. The API documentation uses the JSON data types defined by W3Schools. The resource documentation describes requests and responses in detail for each endpoint. ### Getting Started 1. Go to our developer portal and create your developer account. 2. While logged into your developer account, request to go live with your developer account. 3. Once Mindbody approves you for live access, activate the link between your developer account and at least one Mindbody business. 4. Go to the API Keys section on the API Credentials page of your developer portal account, and create an API Key. 5. Use the POST subscription endpoint to create a subscription for one or more events. 6. Use the PATCH subscription endpoint to activate your subscription. 7. Thoroughly test your application. 8. Optionally, use the DELETE subscription endpoint to deactivate the webhook you used for testing. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.