List notification subscriptions
Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to list notification subscriptions.
Java
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package shopping.merchant.samples.notifications.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.rpc.ApiException;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.notifications.v1.ListNotificationSubscriptionsRequest;
import com.google.shopping.merchant.notifications.v1.NotificationSubscription;
import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient;
import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient.ListNotificationSubscriptionsPagedResponse;
import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to list NotificationSubscriptions for a given Merchant Center
* account.
*/
public class ListNotificationSubscriptionsSample {
public static void listNotificationSubscriptions(Config config) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
NotificationsApiServiceSettings notificationsApiServiceSettings =
NotificationsApiServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Calls the API and catches and prints any network failures/errors.
try (NotificationsApiServiceClient notificationsApiServiceClient =
NotificationsApiServiceClient.create(notificationsApiServiceSettings)) {
// The parent has the format: accounts/{account}
String parent = "accounts/" + config.getAccountId().toString();
ListNotificationSubscriptionsRequest request =
ListNotificationSubscriptionsRequest.newBuilder().setParent(parent).build();
System.out.println("Sending list Notification Subscriptions request:");
ListNotificationSubscriptionsPagedResponse response =
notificationsApiServiceClient.listNotificationSubscriptions(request);
int count = 0;
for (NotificationSubscription notificationSubscription : response.iterateAll()) {
System.out.println(notificationSubscription);
count++;
}
System.out.print("The following count of notification subscriptions were returned: ");
System.out.println(count);
} catch (ApiException e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listNotificationSubscriptions(config);
}
}
PHP
<?php
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once __DIR__ . '/../../../vendor/autoload.php';
require_once __DIR__ . '/../../Authentication/Authentication.php';
require_once __DIR__ . '/../../Authentication/Config.php';
use Google\ApiCore\ApiException;
use Google\Shopping\Merchant\Notifications\V1\Client\NotificationsApiServiceClient;
use Google\Shopping\Merchant\Notifications\V1\ListNotificationSubscriptionsRequest;
/**
* Lists Notification Subscriptions.
*/
class ListNotificationSubscriptions
{
/**
* Helper function to construct the parent resource name.
* @param $accountId
* The Merchant Center account ID.
* @return string
* The parent resource name.
*/
private static function getParent($accountId)
{
return "accounts/" . $accountId;
}
/**
* Lists Notification Subscriptions for a given Merchant Center account.
*
* @param array $config
* Configuration data for authentication and account details.
* @throws ApiException
*/
public static function listNotificationSubscriptionsSample($config): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Set up client options.
$options = ['credentials' => $credentials];
// Create a client instance.
$notificationsApiServiceClient = new NotificationsApiServiceClient($options);
// Construct the parent resource name.
$parent = self::getParent($config['accountId']);
// Create the request object.
$request = new ListNotificationSubscriptionsRequest(['parent' => $parent]);
print "Sending list Notification Subscriptions request:\n";
// Make the API call.
try {
$response = $notificationsApiServiceClient->listNotificationSubscriptions($request);
$count = 0;
foreach ($response->iterateAllElements() as $subscription) {
print_r($subscription);
$count++;
}
print "The following count of notification subscriptions were returned: " . $count . "\n";
} catch (ApiException $e) {
print "Request failed:\n";
print $e->getMessage() . "\n";
}
}
/**
* Execute the sample.
*
* @throws ApiException
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::listNotificationSubscriptionsSample($config);
}
}
// Run the script.
$sample = new ListNotificationSubscriptions();
$sample->callSample();
Python
# -*- coding: utf-8 -*-
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A module to list NotificationSubscriptions."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_notifications_v1 import ListNotificationSubscriptionsRequest
from google.shopping.merchant_notifications_v1 import NotificationsApiServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
_PARENT = f"accounts/{_ACCOUNT}"
def list_notification_subscriptions():
"""Lists NotificationSubscriptions for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = NotificationsApiServiceClient(credentials=credentials)
# Creates the request.
request = ListNotificationSubscriptionsRequest(parent=_PARENT)
print("Sending list Notification Subscriptions request:")
# Makes the request and catches and prints any error messages.
try:
response = client.list_notification_subscriptions(request=request)
count = 0
for notification_subscription in response:
print(notification_subscription)
count += 1
print(
"The following count of notification subscriptions were returned:"
f" {count}"
)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
list_notification_subscriptions()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-13 UTC.
[null,null,["Last updated 2025-08-13 UTC."],[],[],null,["# List notification subscriptions\n\nMerchant API code sample to list notification subscriptions. \n\n### Java\n\n // Copyright 2025 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.notifications.v1;\n\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.api.gax.rpc.ApiException;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.notifications.v1.ListNotificationSubscriptionsRequest;\n import com.google.shopping.merchant.notifications.v1.NotificationSubscription;\n import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient;\n import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient.ListNotificationSubscriptionsPagedResponse;\n import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /**\n * This class demonstrates how to list NotificationSubscriptions for a given Merchant Center\n * account.\n */\n public class ListNotificationSubscriptionsSample {\n\n public static void listNotificationSubscriptions(Config config) throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Creates service settings using the credentials retrieved above.\n NotificationsApiServiceSettings notificationsApiServiceSettings =\n NotificationsApiServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Calls the API and catches and prints any network failures/errors.\n try (NotificationsApiServiceClient notificationsApiServiceClient =\n NotificationsApiServiceClient.create(notificationsApiServiceSettings)) {\n\n // The parent has the format: accounts/{account}\n String parent = \"accounts/\" + config.getAccountId().toString();\n\n ListNotificationSubscriptionsRequest request =\n ListNotificationSubscriptionsRequest.newBuilder().setParent(parent).build();\n\n System.out.println(\"Sending list Notification Subscriptions request:\");\n ListNotificationSubscriptionsPagedResponse response =\n notificationsApiServiceClient.listNotificationSubscriptions(request);\n int count = 0;\n for (NotificationSubscription notificationSubscription : response.iterateAll()) {\n System.out.println(notificationSubscription);\n count++;\n }\n System.out.print(\"The following count of notification subscriptions were returned: \");\n System.out.println(count);\n\n } catch (ApiException e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n listNotificationSubscriptions(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/notifications/v1/ListNotificationSubscriptionsSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n require_once __DIR__ . '/../../../vendor/autoload.php';\n require_once __DIR__ . '/../../Authentication/Authentication.php';\n require_once __DIR__ . '/../../Authentication/Config.php';\n use Google\\ApiCore\\ApiException;\n use Google\\Shopping\\Merchant\\Notifications\\V1\\Client\\NotificationsApiServiceClient;\n use Google\\Shopping\\Merchant\\Notifications\\V1\\ListNotificationSubscriptionsRequest;\n\n /**\n * Lists Notification Subscriptions.\n */\n class ListNotificationSubscriptions\n {\n /**\n * Helper function to construct the parent resource name.\n * @param $accountId\n * The Merchant Center account ID.\n * @return string\n * The parent resource name.\n */\n private static function getParent($accountId)\n {\n return \"accounts/\" . $accountId;\n }\n\n /**\n * Lists Notification Subscriptions for a given Merchant Center account.\n *\n * @param array $config\n * Configuration data for authentication and account details.\n * @throws ApiException\n */\n public static function listNotificationSubscriptionsSample($config): void\n {\n // Get OAuth credentials.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Set up client options.\n $options = ['credentials' =\u003e $credentials];\n\n // Create a client instance.\n $notificationsApiServiceClient = new NotificationsApiServiceClient($options);\n\n // Construct the parent resource name.\n $parent = self::getParent($config['accountId']);\n\n // Create the request object.\n $request = new ListNotificationSubscriptionsRequest(['parent' =\u003e $parent]);\n\n print \"Sending list Notification Subscriptions request:\\n\";\n\n // Make the API call.\n try {\n $response = $notificationsApiServiceClient-\u003elistNotificationSubscriptions($request);\n $count = 0;\n foreach ($response-\u003eiterateAllElements() as $subscription) {\n print_r($subscription);\n $count++;\n }\n print \"The following count of notification subscriptions were returned: \" . $count . \"\\n\";\n } catch (ApiException $e) {\n print \"Request failed:\\n\";\n print $e-\u003egetMessage() . \"\\n\";\n }\n }\n\n /**\n * Execute the sample.\n *\n * @throws ApiException\n */\n public function callSample(): void\n {\n $config = Config::generateConfig();\n self::listNotificationSubscriptionsSample($config);\n }\n }\n\n // Run the script.\n $sample = new ListNotificationSubscriptions();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/notifications/v1/ListNotificationSubscriptionsSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2025 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \"\"\"A module to list NotificationSubscriptions.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_notifications_v1 import ListNotificationSubscriptionsRequest\n from google.shopping.merchant_notifications_v1 import NotificationsApiServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n _PARENT = f\"accounts/{_ACCOUNT}\"\n\n\n def list_notification_subscriptions():\n \"\"\"Lists NotificationSubscriptions for a given Merchant Center account.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = NotificationsApiServiceClient(credentials=credentials)\n\n # Creates the request.\n request = ListNotificationSubscriptionsRequest(parent=_PARENT)\n\n print(\"Sending list Notification Subscriptions request:\")\n # Makes the request and catches and prints any error messages.\n try:\n response = client.list_notification_subscriptions(request=request)\n count = 0\n for notification_subscription in response:\n print(notification_subscription)\n count += 1\n print(\n \"The following count of notification subscriptions were returned:\"\n f\" {count}\"\n )\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n list_notification_subscriptions() \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/notifications/v1/list_notification_subscriptions_sample.py"]]