You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Create a service hook subscription programmatically
3
-
description: Use service hooks to set up actions to take when specific events occur in Azure DevOps.
2
+
title: Create a Service Hook Subscription Programmatically
3
+
description: Find out how to programmatically create a service hook subscription that prescribes an action to take when a specified event occurs in Azure DevOps.
4
4
ms.assetid: 0614F217-4F4E-45DC-A50C-B9FF81F8A5BD
5
5
ms.custom: engagement-fy23
6
6
ms.subservice: azure-devops-service-hooks
7
-
ms.topic: conceptual
7
+
ms.topic: how-to
8
8
ms.author: chcomley
9
9
author: chcomley
10
10
monikerRange: '<= azure-devops'
11
-
ms.date: 10/14/2022
11
+
ms.date: 06/25/2025
12
+
# customer intent: As a developer, I want to create a service hook subscription programmatically so that I can automate tasks in other services when events happen in my Azure DevOps project.
12
13
---
13
14
14
15
# Create a service hook subscription programmatically
[!INCLUDE [Azure DevOps Services | Azure DevOps Server 2022 | Azure DevOps Server 2020](../includes/version-gt-eq-2020.md)]
17
18
18
-
Using the [Subscriptions REST APIs](/rest/api/azure/devops/hooks/) , you can programmatically create a subscription that performs an action on an external/consumer service when a specific event occurs in an Azure DevOps project. For example, you can create a subscription to notify your service when a build fails.
19
+
You can use a subscription to perform an action on an external or consumer service when a specific event occurs in an Azure DevOps project. For example, a subscription can notify your service when a build fails.
19
20
20
-
Supported events:
21
-
22
-
- Build completed
23
-
- Code pushed (for Git projects)
24
-
- Pull request create or updated (for Git projects)
25
-
- Code checked in (TFVC projects)
26
-
- Work item created, updated, deleted, restored or commented on
27
-
28
-
You can configure filters on your subscriptions to control which events trigger an action. For example, you can filter the build completed event based on the build status. For a complete set of supported events and filter options, see the [Event reference](./events.md).
29
-
30
-
For a complete set of supported consumer services and actions, see the [Consumer reference](./consumers.md).
21
+
To create a subscription programmatically, you can use the [Subscriptions REST APIs](/rest/api/azure/devops/hooks/). This article provides a sample request and sample code for creating a subscription.
|**Data**|- Project ID. Use the [Project REST API](/rest/api/azure/devops/core/projects) to get the project ID.<br>- Event ID and settings. See the [Event reference](./events.md).<br>- Consumer and action IDs and settings. See the [Consumer reference](./consumers.md).|
28
+
|**Data**|- Project ID. Use the [Project REST API](/rest/api/azure/devops/core/projects) to get the project ID.<br>- Event ID and settings. See [Service hook events](events.md).<br>- Consumer and action IDs and settings. See [Service hook consumers](consumers.md).|
29
+
30
+
## Supported events
31
+
32
+
Azure DevOps provides support for numerous trigger events. Examples include the following events:
33
+
34
+
- Build completed
35
+
- Code pushed (for Git projects)
36
+
- Pull request created or updated (for Git projects)
37
+
- Code checked in (for Team Foundation Version Control projects)
38
+
- Work item created, updated, deleted, restored, or commented on
39
+
40
+
To control which events trigger an action, you can configure filters on your subscriptions. For example, you can filter the build completed event based on the build status.
41
+
42
+
- For a complete set of supported events and filter options, see [Service hook events](events.md).
43
+
- For a complete set of supported consumer services and actions, see [Service hook consumers](consumers.md).
38
44
39
-
## Create the request
45
+
## Create a request
40
46
41
-
Construct the body of the HTTP POST request to create the subscription based on the project ID, event, consumer, and action.
47
+
When you create a subscription, you use the body of an HTTP POST request to specify the project ID, event, consumer, action, and related settings.
42
48
43
-
See the following example request for creating a subscription that causes a build event to POST to `https://myservice/event`when the build `WebSite.CI` fails.
49
+
You can use the following request to create a subscription for a build completed event. In this example, when the `WebSite.CI`build fails, the subscription sends a POST request to `https://myservice/event`.
44
50
45
51
**Request**
46
-
```js
52
+
53
+
```json
47
54
{
48
55
"publisherId": "tfs",
49
56
"eventType": "build.complete",
@@ -53,7 +60,7 @@ See the following example request for creating a subscription that causes a buil
@@ -101,117 +109,106 @@ If the subscription request fails, you get an HTTP response code of 400 with a m
101
109
102
110
### What happens when the event occurs?
103
111
104
-
When an event occurs, all enabled subscriptions in the project are evaluated, and the consumer action is performed for all matching subscriptions.
112
+
When an event occurs, all enabled subscriptions in the project are evaluated. Then the consumer action is performed for all matching subscriptions.
105
113
106
114
### Resource versions (advanced)
107
115
108
116
Resource versioning is applicable when an API is in preview. For most scenarios, specifying `1.0` as the resource version is the safest route.
109
117
110
-
The event payload sent to certain consumers, like Webhooks, Azure Service Bus, and Azure Storage, includes a JSON representation of subject resource (for example, a build or work item). The representation of this resource can have different forms or versions.
118
+
The event payload sent to certain consumers includes a JSON representation of a subject resource. For instance, the payload sent to webhooks, Azure Service Bus, and Azure Storage includes information about a build or work item. The representation of this resource can have various forms or versions.
119
+
120
+
You can specify the version of the resource that you want to send to the consumer service via the `resourceVersion` field on the subscription.
111
121
112
-
You can specify the version of the resource that you want to have sent to the consumer service via the `resourceVersion` field on the subscription.
113
-
The resource version is the same as the [API version](../integrate/concepts/rest-api-versioning.md). Not specifying a resource version means "latest released". You should always specify a resource version, which ensures a consistent event payload over time.
122
+
The resource version is the same as the [API version](../integrate/concepts/rest-api-versioning.md). If you don't specify a resource version, the latest version, `latest released`, is used. To help ensure a consistent event payload over time, always specify a resource version.
114
123
115
124
## FAQs
125
+
116
126
### Q: Are there services that I can subscribe to manually?
117
127
118
-
A: Yes. For more information about the services that you can subscribe to from the administration page for a project, see the [Overview](./overview.md).
128
+
A: Yes. For more information about the services that you can subscribe to from a project administration page, see [Integrate with service hooks](overview.md).
119
129
120
130
### Q: Are there C# libraries that I can use to create subscriptions?
121
131
122
-
A: No, but here's a sample to help you get started.
132
+
A: No, but here's a sample to help you get started. For authentication to Azure DevOps, the following code uses a personal access token (PAT) that's stored in Azure Key Vault. In a production environment, use a more secure authentication method. For more information, see [Choose the right authentication mechanism](../integrate/get-started/authentication/authentication-guidance.md).
0 commit comments