Get a notification subscription
Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to get a notification subscription.
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.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.notifications.v1.GetNotificationSubscriptionRequest;
import com.google.shopping.merchant.notifications.v1.NotificationSubscription;
import com.google.shopping.merchant.notifications.v1.NotificationSubscriptionName;
import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient;
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 get a specific Notification Subscription for a given Merchant
* Center account.
*/
public class GetNotificationSubscriptionSample {
public static void getNotificationSubscription(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();
// Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID.
String notificationSubscriptionId = "YOUR_NOTIFICATION_SUBSCRIPTION_ID";
// Creates notification subscription name to identify the subscription.
String name =
NotificationSubscriptionName.newBuilder()
.setAccount(config.getAccountId().toString())
.setNotificationSubscription(notificationSubscriptionId)
.build()
.toString();
// Calls the API and catches and prints any network failures/errors.
try (NotificationsApiServiceClient notificationSubscriptionServiceClient =
NotificationsApiServiceClient.create(notificationsApiServiceSettings)) {
// The name has the format:
// accounts/{account}/notificationSubscription/{notification_subscription}
GetNotificationSubscriptionRequest request =
GetNotificationSubscriptionRequest.newBuilder().setName(name).build();
System.out.println("Sending get Notification Subscription request:");
NotificationSubscription response =
notificationSubscriptionServiceClient.getNotificationSubscription(request);
System.out.println("Retrieved Notification Subscription below:");
System.out.println(response);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
getNotificationSubscription(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\GetNotificationSubscriptionRequest;
/**
* Gets a Notification Subscription.
*/
class GetNotificationSubscription
{
/**
* Gets a specific Notification Subscription for a given Merchant Center account.
*
* @param array $config
* Configuration data for authentication and account details.
* @param string $notificationSubscriptionId
* The ID of the notification subscription to retrieve.
* @throws ApiException
*/
public static function getNotificationSubscriptionSample($config, $notificationSubscriptionId): void
{
// Get OAuth credentials.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Set up client options.
$options = ['credentials' => $credentials];
// Create a client instance.
$notificationsApiServiceClient = new NotificationsApiServiceClient($options);
// Construct the resource name.
$name = "accounts/" . $config['accountId'] .
"/notificationsubscriptions/" . $notificationSubscriptionId;
// Create the request object.
$request = new GetNotificationSubscriptionRequest(['name' => $name]);
print "Sending get Notification Subscription request:\n";
// Make the API call.
try {
$response = $notificationsApiServiceClient->getNotificationSubscription($request);
print "Retrieved Notification Subscription below:\n";
print_r($response);
} catch (ApiException $e) {
print "Request failed:\n";
print $e->getMessage() . "\n";
}
}
/**
* Execute the sample.
*
* @throws ApiException
*/
public function callSample(): void
{
$config = Config::generateConfig();
// Replace with the actual notification subscription ID.
$notificationSubscriptionId = "YOUR_NOTIFICATION_SUBSCRIPTION_ID";
self::getNotificationSubscriptionSample($config, $notificationSubscriptionId);
}
}
// Run the script.
$sample = new GetNotificationSubscription();
$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 get NotificationSubscription."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_notifications_v1 import GetNotificationSubscriptionRequest
from google.shopping.merchant_notifications_v1 import NotificationsApiServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def get_notification_subscription():
"""Gets a specific Notification Subscription for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = NotificationsApiServiceClient(credentials=credentials)
# Replace "YOUR_NOTIFICATION_SUBSCRIPTION_ID" with the actual ID.
notification_subscription_id = "YOUR_NOTIFICATION_SUBSCRIPTION_ID"
# Creates notification subscription name to identify the subscription.
name = f"accounts/{_ACCOUNT}/notificationsubscriptions/{notification_subscription_id}"
# Creates the request.
request = GetNotificationSubscriptionRequest(name=name)
print("Sending get Notification Subscription request:")
# Makes the request and catches and prints any error messages.
try:
response = client.get_notification_subscription(request=request)
print("Retrieved Notification Subscription below:")
print(response)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
get_notification_subscription()
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,["# Get a notification subscription\n\nMerchant API code sample to get a notification subscription. \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.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.notifications.v1.GetNotificationSubscriptionRequest;\n import com.google.shopping.merchant.notifications.v1.NotificationSubscription;\n import com.google.shopping.merchant.notifications.v1.NotificationSubscriptionName;\n import com.google.shopping.merchant.notifications.v1.NotificationsApiServiceClient;\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 get a specific Notification Subscription for a given Merchant\n * Center account.\n */\n public class GetNotificationSubscriptionSample {\n\n public static void getNotificationSubscription(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 // Replace \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\" with the actual ID.\n String notificationSubscriptionId = \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\";\n\n // Creates notification subscription name to identify the subscription.\n String name =\n NotificationSubscriptionName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setNotificationSubscription(notificationSubscriptionId)\n .build()\n .toString();\n\n // Calls the API and catches and prints any network failures/errors.\n try (NotificationsApiServiceClient notificationSubscriptionServiceClient =\n NotificationsApiServiceClient.create(notificationsApiServiceSettings)) {\n\n // The name has the format:\n // accounts/{account}/notificationSubscription/{notification_subscription}\n GetNotificationSubscriptionRequest request =\n GetNotificationSubscriptionRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending get Notification Subscription request:\");\n NotificationSubscription response =\n notificationSubscriptionServiceClient.getNotificationSubscription(request);\n\n System.out.println(\"Retrieved Notification Subscription below:\");\n System.out.println(response);\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n getNotificationSubscription(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/notifications/v1/GetNotificationSubscriptionSample.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\\GetNotificationSubscriptionRequest;\n\n /**\n * Gets a Notification Subscription.\n */\n class GetNotificationSubscription\n {\n /**\n * Gets a specific Notification Subscription for a given Merchant Center account.\n *\n * @param array $config\n * Configuration data for authentication and account details.\n * @param string $notificationSubscriptionId\n * The ID of the notification subscription to retrieve.\n * @throws ApiException\n */\n public static function getNotificationSubscriptionSample($config, $notificationSubscriptionId): 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 resource name.\n $name = \"accounts/\" . $config['accountId'] .\n \"/notificationsubscriptions/\" . $notificationSubscriptionId;\n\n // Create the request object.\n $request = new GetNotificationSubscriptionRequest(['name' =\u003e $name]);\n\n print \"Sending get Notification Subscription request:\\n\";\n\n // Make the API call.\n try {\n $response = $notificationsApiServiceClient-\u003egetNotificationSubscription($request);\n print \"Retrieved Notification Subscription below:\\n\";\n print_r($response);\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 // Replace with the actual notification subscription ID.\n $notificationSubscriptionId = \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\";\n\n self::getNotificationSubscriptionSample($config, $notificationSubscriptionId);\n }\n }\n\n // Run the script.\n $sample = new GetNotificationSubscription();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/notifications/v1/GetNotificationSubscriptionSample.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 get NotificationSubscription.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_notifications_v1 import GetNotificationSubscriptionRequest\n from google.shopping.merchant_notifications_v1 import NotificationsApiServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def get_notification_subscription():\n \"\"\"Gets a specific Notification Subscription 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 # Replace \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\" with the actual ID.\n notification_subscription_id = \"YOUR_NOTIFICATION_SUBSCRIPTION_ID\"\n\n # Creates notification subscription name to identify the subscription.\n name = f\"accounts/{_ACCOUNT}/notificationsubscriptions/{notification_subscription_id}\"\n\n # Creates the request.\n request = GetNotificationSubscriptionRequest(name=name)\n\n print(\"Sending get Notification Subscription request:\")\n # Makes the request and catches and prints any error messages.\n try:\n response = client.get_notification_subscription(request=request)\n print(\"Retrieved Notification Subscription below:\")\n print(response)\n except RuntimeError as e:\n print(e)\n\n\n if __name__ == \"__main__\":\n get_notification_subscription() \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/notifications/v1/get_notification_subscription_sample.py"]]