# Per-Service Webhook Resource

The Per-Service Webhook resource allows you to control the effects of webhooks in a particular [Conversation Service](/docs/conversations/api/service-resource). The webhooks will only fire for activity at the service-level.

Services allow you to:

* Create multiple, distinct environments (such as dev, stage, and prod) under a single Twilio account
* Scope access to resources through both the REST and client APIs
* Configure different service instances with specific behaviors

Every service can have unique webhook targets. This means you can include different metadata in the URLs or even trigger different behavior for different services.

Webhook targets for the Service Instance (the URL that Twilio will invoke) are configured in the Twilio Console.

If configured, service-scoped webhooks will override your global webhook settings such that only the service-scoped hooks will fire. This applies only to the services where service-level hooks are configured. See [Conversations Webhooks](/docs/conversations/conversations-webhooks)for more information.

## Webhook Properties

```json
{"type":"object","refName":"conversations.v1.service.service_configuration.service_webhook_configuration","modelName":"conversations_v1_service_service_configuration_service_webhook_configuration","properties":{"account_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^AC[0-9a-fA-F]{32}$","nullable":true,"description":"The unique ID of the [Account](/docs/iam/api/account) responsible for this service."},"chat_service_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^IS[0-9a-fA-F]{32}$","nullable":true,"description":"The unique ID of the [Conversation Service](/docs/conversations/api/service-resource) this conversation belongs to."},"pre_webhook_url":{"type":"string","format":"uri","nullable":true,"description":"The absolute url the pre-event webhook request should be sent to."},"post_webhook_url":{"type":"string","format":"uri","nullable":true,"description":"The absolute url the post-event webhook request should be sent to."},"filters":{"type":"array","nullable":true,"description":"The list of events that your configured webhook targets will receive. Events not configured here will not fire. Possible values are `onParticipantAdd`, `onParticipantAdded`, `onDeliveryUpdated`, `onConversationUpdated`, `onConversationRemove`, `onParticipantRemove`, `onConversationUpdate`, `onMessageAdd`, `onMessageRemoved`, `onParticipantUpdated`, `onConversationAdded`, `onMessageAdded`, `onConversationAdd`, `onConversationRemoved`, `onParticipantUpdate`, `onMessageRemove`, `onMessageUpdated`, `onParticipantRemoved`, `onMessageUpdate` or `onConversationStateUpdated`.","items":{"type":"string"}},"method":{"type":"string","enum":["GET","POST"],"description":"The HTTP method to be used when sending a webhook request. One of `GET` or `POST`.","refName":"service_webhook_configuration_enum_method","modelName":"service_webhook_configuration_enum_method"},"url":{"type":"string","format":"uri","nullable":true,"description":"An absolute API resource URL for this webhook."}}}
```

## Fetch a ServiceWebhookConfiguration resource

`GET https://conversations.twilio.com/v1/Services/{ChatServiceSid}/Configuration/Webhooks`

### Path parameters

```json
[{"name":"ChatServiceSid","in":"path","description":"The unique ID of the [Conversation Service](/docs/conversations/api/service-resource) this conversation belongs to.","schema":{"type":"string","minLength":34,"maxLength":34,"pattern":"^IS[0-9a-fA-F]{32}$"},"required":true}]
```

Fetch a Service Webhook

```js
// Download the helper library from https://www.twilio.com/docs/node/install
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);

async function fetchServiceWebhookConfiguration() {
  const webhook = await client.conversations.v1
    .services("ISXXXXXXXXXXXXXXXXXXXXXX")
    .configuration.webhooks()
    .fetch();

  console.log(webhook.accountSid);
}

fetchServiceWebhookConfiguration();
```

```python
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)

webhook = (
    client.conversations.v1.services("ISXXXXXXXXXXXXXXXXXXXXXX")
    .configuration.webhooks()
    .fetch()
)

print(webhook.account_sid)
```

```csharp
// Install the C# / .NET helper library from twilio.com/docs/csharp/install

using System;
using Twilio;
using Twilio.Rest.Conversations.V1.Service.Configuration;
using System.Threading.Tasks;

class Program {
    public static async Task Main(string[] args) {
        // Find your Account SID and Auth Token at twilio.com/console
        // and set the environment variables. See http://twil.io/secure
        string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

        TwilioClient.Init(accountSid, authToken);

        var webhook =
            await WebhookResource.FetchAsync(pathChatServiceSid: "ISXXXXXXXXXXXXXXXXXXXXXX");

        Console.WriteLine(webhook.AccountSid);
    }
}
```

```java
// Install the Java helper library from twilio.com/docs/java/install

import com.twilio.Twilio;
import com.twilio.rest.conversations.v1.service.configuration.Webhook;

public class Example {
    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
    public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");

    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Webhook webhook = Webhook.fetcher("ISXXXXXXXXXXXXXXXXXXXXXX").fetch();

        System.out.println(webhook.getAccountSid());
    }
}
```

```go
// Download the helper library from https://www.twilio.com/docs/go/install
package main

import (
	"fmt"
	"github.com/twilio/twilio-go"
	"os"
)

func main() {
	// Find your Account SID and Auth Token at twilio.com/console
	// and set the environment variables. See http://twil.io/secure
	// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
	client := twilio.NewRestClient()

	resp, err := client.ConversationsV1.FetchServiceWebhookConfiguration("ISXXXXXXXXXXXXXXXXXXXXXX")
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	} else {
		if resp.AccountSid != nil {
			fmt.Println(*resp.AccountSid)
		} else {
			fmt.Println(resp.AccountSid)
		}
	}
}
```

```php
<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once "/path/to/vendor/autoload.php";

use Twilio\Rest\Client;

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$webhook = $twilio->conversations->v1
    ->services("ISXXXXXXXXXXXXXXXXXXXXXX")
    ->configuration->webhooks()
    ->fetch();

print $webhook->accountSid;
```

```ruby
# Download the helper library from https://www.twilio.com/docs/ruby/install
require 'twilio-ruby'

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_AUTH_TOKEN']
@client = Twilio::REST::Client.new(account_sid, auth_token)

webhook = @client
          .conversations
          .v1
          .services('ISXXXXXXXXXXXXXXXXXXXXXX')
          .configuration
          .webhooks
          .fetch

puts webhook.account_sid
```

```bash
# Install the twilio-cli from https://twil.io/cli

twilio api:conversations:v1:services:configuration:webhooks:fetch \
   --chat-service-sid ISXXXXXXXXXXXXXXXXXXXXXX
```

```bash
curl -X GET "https://conversations.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXX/Configuration/Webhooks" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

```json
{
  "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "chat_service_sid": "ISXXXXXXXXXXXXXXXXXXXXXX",
  "pre_webhook_url": "https://www.example.com/pre",
  "post_webhook_url": "https://www.example.com/post",
  "filters": [
    "onMessageRemove",
    "onParticipantAdd"
  ],
  "method": "POST",
  "url": "https://conversations.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Configuration/Webhooks"
}
```

## Update a ServiceWebhookConfiguration resource

`POST https://conversations.twilio.com/v1/Services/{ChatServiceSid}/Configuration/Webhooks`

### Path parameters

```json
[{"name":"ChatServiceSid","in":"path","description":"The unique ID of the [Conversation Service](/docs/conversations/api/service-resource) this conversation belongs to.","schema":{"type":"string","minLength":34,"maxLength":34,"pattern":"^IS[0-9a-fA-F]{32}$"},"required":true}]
```

### Request body parameters

```json
{"schema":{"type":"object","title":"UpdateServiceWebhookConfigurationRequest","properties":{"PreWebhookUrl":{"type":"string","format":"uri","description":"The absolute url the pre-event webhook request should be sent to."},"PostWebhookUrl":{"type":"string","format":"uri","description":"The absolute url the post-event webhook request should be sent to."},"Filters":{"type":"array","description":"The list of events that your configured webhook targets will receive. Events not configured here will not fire. Possible values are `onParticipantAdd`, `onParticipantAdded`, `onDeliveryUpdated`, `onConversationUpdated`, `onConversationRemove`, `onParticipantRemove`, `onConversationUpdate`, `onMessageAdd`, `onMessageRemoved`, `onParticipantUpdated`, `onConversationAdded`, `onMessageAdded`, `onConversationAdd`, `onConversationRemoved`, `onParticipantUpdate`, `onMessageRemove`, `onMessageUpdated`, `onParticipantRemoved`, `onMessageUpdate` or `onConversationStateUpdated`.","items":{"type":"string"}},"Method":{"type":"string","description":"The HTTP method to be used when sending a webhook request. One of `GET` or `POST`."}}},"examples":{"update":{"value":{"lang":"json","value":"{\n  \"PreWebhookUrl\": \"https://www.example.com/pre\",\n  \"PostWebhookUrl\": \"https://www.example.com/post\",\n  \"Filters\": [\n    \"onMessageRemoved\",\n    \"onParticipantAdded\"\n  ],\n  \"Method\": \"GET\"\n}","meta":"","code":"{\n  \"PreWebhookUrl\": \"https://www.example.com/pre\",\n  \"PostWebhookUrl\": \"https://www.example.com/post\",\n  \"Filters\": [\n    \"onMessageRemoved\",\n    \"onParticipantAdded\"\n  ],\n  \"Method\": \"GET\"\n}","tokens":[["{","#C9D1D9"],"\n  ",["\"PreWebhookUrl\"","#7EE787"],[":","#C9D1D9"]," ",["\"https://www.example.com/pre\"","#A5D6FF"],[",","#C9D1D9"],"\n  ",["\"PostWebhookUrl\"","#7EE787"],[":","#C9D1D9"]," ",["\"https://www.example.com/post\"","#A5D6FF"],[",","#C9D1D9"],"\n  ",["\"Filters\"","#7EE787"],[": [","#C9D1D9"],"\n    ",["\"onMessageRemoved\"","#A5D6FF"],[",","#C9D1D9"],"\n    ",["\"onParticipantAdded\"","#A5D6FF"],"\n  ",["],","#C9D1D9"],"\n  ",["\"Method\"","#7EE787"],[":","#C9D1D9"]," ",["\"GET\"","#A5D6FF"],"\n",["}","#C9D1D9"]],"annotations":[],"themeName":"github-dark","style":{"color":"#c9d1d9","background":"#0d1117"}}}},"encodingType":"application/x-www-form-urlencoded","conditionalParameterMap":{}}
```

Update a Service Webhook

```js
// Download the helper library from https://www.twilio.com/docs/node/install
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);

async function updateServiceWebhookConfiguration() {
  const webhook = await client.conversations.v1
    .services("ISXXXXXXXXXXXXXXXXXXXXXX")
    .configuration.webhooks()
    .update({
      filters: ["onConversationUpdated", "onMessageRemoved"],
      method: "POST",
      postWebhookUrl: "https://example.com/archive-every-action",
      preWebhookUrl: "https://example.com/filtering-and-permissions",
    });

  console.log(webhook.accountSid);
}

updateServiceWebhookConfiguration();
```

```python
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)

service_webhook_configuration = (
    client.conversations.v1.services("ISXXXXXXXXXXXXXXXXXXXXXX")
    .configuration.webhooks()
    .update(
        filters=["onConversationUpdated", "onMessageRemoved"],
        method="POST",
        post_webhook_url="https://example.com/archive-every-action",
        pre_webhook_url="https://example.com/filtering-and-permissions",
    )
)

print(service_webhook_configuration.account_sid)
```

```csharp
// Install the C# / .NET helper library from twilio.com/docs/csharp/install

using System;
using Twilio;
using Twilio.Rest.Conversations.V1.Service.Configuration;
using System.Threading.Tasks;
using System.Collections.Generic;

class Program {
    public static async Task Main(string[] args) {
        // Find your Account SID and Auth Token at twilio.com/console
        // and set the environment variables. See http://twil.io/secure
        string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

        TwilioClient.Init(accountSid, authToken);

        var webhook = await WebhookResource.UpdateAsync(
            filters: new List<string> { "onConversationUpdated", "onMessageRemoved" },
            method: "POST",
            postWebhookUrl: new Uri("https://example.com/archive-every-action"),
            preWebhookUrl: new Uri("https://example.com/filtering-and-permissions"),
            pathChatServiceSid: "ISXXXXXXXXXXXXXXXXXXXXXX");

        Console.WriteLine(webhook.AccountSid);
    }
}
```

```java
// Install the Java helper library from twilio.com/docs/java/install

import java.net.URI;
import java.util.Arrays;
import com.twilio.Twilio;
import com.twilio.rest.conversations.v1.service.configuration.Webhook;

public class Example {
    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
    public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");

    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Webhook webhook = Webhook.updater("ISXXXXXXXXXXXXXXXXXXXXXX")
                              .setFilters(Arrays.asList("onConversationUpdated", "onMessageRemoved"))
                              .setMethod("POST")
                              .setPostWebhookUrl(URI.create("https://example.com/archive-every-action"))
                              .setPreWebhookUrl(URI.create("https://example.com/filtering-and-permissions"))
                              .update();

        System.out.println(webhook.getAccountSid());
    }
}
```

```go
// Download the helper library from https://www.twilio.com/docs/go/install
package main

import (
	"fmt"
	"github.com/twilio/twilio-go"
	conversations "github.com/twilio/twilio-go/rest/conversations/v1"
	"os"
)

func main() {
	// Find your Account SID and Auth Token at twilio.com/console
	// and set the environment variables. See http://twil.io/secure
	// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
	client := twilio.NewRestClient()

	params := &conversations.UpdateServiceWebhookConfigurationParams{}
	params.SetFilters([]string{
		"onConversationUpdated",
		"onMessageRemoved",
	})
	params.SetMethod("POST")
	params.SetPostWebhookUrl("https://example.com/archive-every-action")
	params.SetPreWebhookUrl("https://example.com/filtering-and-permissions")

	resp, err := client.ConversationsV1.UpdateServiceWebhookConfiguration("ISXXXXXXXXXXXXXXXXXXXXXX",
		params)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	} else {
		if resp.AccountSid != nil {
			fmt.Println(*resp.AccountSid)
		} else {
			fmt.Println(resp.AccountSid)
		}
	}
}
```

```php
<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once "/path/to/vendor/autoload.php";

use Twilio\Rest\Client;

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$service_webhook_configuration = $twilio->conversations->v1
    ->services("ISXXXXXXXXXXXXXXXXXXXXXX")
    ->configuration->webhooks()
    ->update([
        "filters" => ["onConversationUpdated", "onMessageRemoved"],
        "method" => "POST",
        "postWebhookUrl" => "https://example.com/archive-every-action",
        "preWebhookUrl" => "https://example.com/filtering-and-permissions",
    ]);

print $service_webhook_configuration->accountSid;
```

```ruby
# Download the helper library from https://www.twilio.com/docs/ruby/install
require 'twilio-ruby'

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_AUTH_TOKEN']
@client = Twilio::REST::Client.new(account_sid, auth_token)

webhook = @client
          .conversations
          .v1
          .services('ISXXXXXXXXXXXXXXXXXXXXXX')
          .configuration
          .webhooks
          .update(
            filters: [
              'onConversationUpdated',
              'onMessageRemoved'
            ],
            method: 'POST',
            post_webhook_url: 'https://example.com/archive-every-action',
            pre_webhook_url: 'https://example.com/filtering-and-permissions'
          )

puts webhook.account_sid
```

```bash
# Install the twilio-cli from https://twil.io/cli

twilio api:conversations:v1:services:configuration:webhooks:update \
   --chat-service-sid ISXXXXXXXXXXXXXXXXXXXXXX \
   --filters onConversationUpdated onMessageRemoved \
   --method POST \
   --post-webhook-url https://example.com/archive-every-action \
   --pre-webhook-url https://example.com/filtering-and-permissions
```

```bash
curl -X POST "https://conversations.twilio.com/v1/Services/ISXXXXXXXXXXXXXXXXXXXXXX/Configuration/Webhooks" \
--data-urlencode "Filters=onConversationUpdated" \
--data-urlencode "Filters=onMessageRemoved" \
--data-urlencode "Method=POST" \
--data-urlencode "PostWebhookUrl=https://example.com/archive-every-action" \
--data-urlencode "PreWebhookUrl=https://example.com/filtering-and-permissions" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

```json
{
  "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "chat_service_sid": "ISXXXXXXXXXXXXXXXXXXXXXX",
  "pre_webhook_url": "https://example.com/filtering-and-permissions",
  "post_webhook_url": "https://example.com/archive-every-action",
  "filters": [
    "onConversationUpdated",
    "onMessageRemoved"
  ],
  "method": "POST",
  "url": "https://conversations.twilio.com/v1/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Configuration/Webhooks"
}
```
