Activities

Learn about the activities resource and how to use it to get a response from an API call.

The activities resource is mainly used to track each operation performed by the user’s own channel or a channel on YouTube, such as commenting on a video, rating a video, or adding a video to a playlist.

List activities

We can call the endpoint by sending a GET request to the following URL:

https://www.googleapis.com/youtube/v3/activities

Note: This resource only offers the listing method for the activities.

Request parameters

Some important request parameters to call the endpoint are as follows:

Name

Type

Category

Description

part

String

Required

This parameter will be used to specify the response field properties, which are contentDetails, id, localizations, player, snippet, and status.

channelId

String

Optional

Used to specify which channel activities are required.

mine

Boolean

Optional

Used to restrict fetches so that data is uploaded by the user.

maxResult

Integer

Optional

Used to fix the number of results that will be fetched.

pageToken

String

Optional

Used to specify the page in the response.

publishedAfter

Datetime

Optional

Used to specify a date and time to fetch results after the specified time.

publishedBefore

Datetime

Optional

Used to specify a date and time to fetch results before the specified time.

regionCode

String

Optional

Used to identify the activities of a specific region.

Click the “Edit” button in the following widget, enter the channel ID in the CHANNEL_ID field, and click the “Save” button. After saving the keyword, click the “Run” button to retrieve the activity results.

Note: Look at the steps in the Find the Chanel ID lesson in the Appendix to find the channel ID.

Press + to interact
// Importing libraries here
import fetch from 'node-fetch';
// Define endpoint URL here
const endpointUrl = new URL('https://www.googleapis.com/youtube/v3/activities');
// Define Header Parameters here
const headerParameters = {
Authorization: 'Bearer {{ACCESS_TOKEN}}',
Accept: 'application/json',
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
part: 'snippet,contentDetails',
channelId: '{{CHANNEL_ID}}',
});
// Setting API call options
const options = {
method: 'GET',
headers: headerParameters,
};
// Function to make API call
async function listingActivities() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
listingActivities();

Let’s take a look at the following code explanation:

  • Line 5: We define a variable with the name endpointUrl in which we pass the endpoint URL.

  • Lines 14–17: We define a variable with the name queryParameters in which we define the channelId parameter. The channelId parameter is used to pass that channel ID. Activities are required.

Note: We can also fetch the user activities by simply using the mine parameter instead of channelId.

Response fields

Name

Type

Description

kind

String

Reflects the resource type of the API call.

etag

ETag

Contains the resource ETag.

nextPageToken

String

Used to fetch the next page in the response result.

prevPageToken

Sstring

Used to fetch the previous page in the response result.

pageInfo

Object

Contains the response page information.

items[]

List

Contains the list of results that matches the request requirement.