Implementation: Subscriptions

  • The YouTube Data API (v3) allows retrieving a list of subscriptions for a channel using the subscriptions.list method, either for the authenticated user's channel (using mine=true) or for another channel (using channelId).

  • To add a channel subscription, you can use the subscriptions.insert method, providing the youtube#channel kind and the target channel's ID in the request body's snippet.resourceId property, while also needing to be authorized using OAuth 2.0.

  • Deleting a channel subscription involves first retrieving the subscription list using subscriptions.list to find the subscription ID, and then using subscriptions.delete with that ID to remove the subscription, which requires OAuth 2.0 authorization.

  • The subscriptions.list method can also be utilized to retrieve a list of channels that subscribe to the currently authenticated user's channel by setting the mySubscribers parameter to true, while needing to be authorized using OAuth 2.0.

The following examples show how to use the YouTube Data API (v3) to perform functions related to subscriptions.

Retrieve a channel's subscriptions

Call the subscriptions.list method to retrieve subscriptions for a particular channel. There are two ways to identify the channel:

See the subscriptions.list method's documentation for code samples.

Add a subscription

Call the subscriptions.insert method to add a channel subscription. This request must be authorized using OAuth 2.0. The request body is a subscription resource that sets the following values:

The API request below subscribes you to the TED channel on YouTube:

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.insert?
        part=snippet

The request body is:

{
  "snippet": {
    "resourceId": {
      "kind": "youtube#channel",
      "videoId": "UCAuUUnT6oDeKwE6v1NGQxug"
    }
  }
}

See the subscriptions.insert method's documentation for code samples.

Delete a subscription

This example deletes a subscription. This request must be authorized using OAuth 2.0. This example has two steps:

  • Step 1: Retrieve the subscriptions for the authenticated user's channel

    Call the subscriptions.list method to retrieve the list of subscriptions. The example above for retrieving a channel's subscriptions explains how to make this request.

    The application calling the API could process the API response to display a list of subscriptions, using each subscription's ID as a key. In the response, each item's id property identifies the subscription ID that uniquely identifies the corresponding subscription. You will use that value to remove an item from the list in the next step.

  • Step 2: Delete a subscription

    Call the subscriptions.delete method to delete a subscription. Set the request's id parameter to the subscription ID for the subscription that you want to remove. This request must be authorized using OAuth 2.0.

    To complete the request in the APIs Explorer, you need to set the id property's value.

    https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.delete?
            id=SUBSCRIPTION_ID

See the subscriptions.delete method's documentation for code samples.

Retrieve a list of subscribers to the authorized user's channel

To retrieve a list of channels that subscribe to the currently authenticated user's channel, call the subscriptions.list method and set the mySubscribers parameter's value to true. The request must be authorized using OAuth 2.0.

https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list?
        part=snippet,contentDetails
        &mySubscribers=true