Merchant API code sample to create 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.CreateNotificationSubscriptionRequest
;
import
com.google.shopping.merchant.notifications.v1.NotificationSubscription
;
import
com.google.shopping.merchant.notifications.v1.NotificationSubscription.NotificationEventType
;
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 create a Notification Subscription for a given Merchant Center
* account.
*/
public
class
CreateNotificationSubscriptionSample
{
public
static
void
createNotificationSubscription
(
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
();
// INSERT HERE the URL to be used to push the notification to.
String
callbackUri
=
"https://www.samplesite.com"
;
// The event type to register for notifications.
NotificationEventType
eventType
=
NotificationEventType
.
PRODUCT_STATUS_CHANGE
;
// Create the NotificationSubscription object.
NotificationSubscription
notificationSubscription
=
NotificationSubscription
.
newBuilder
()
// Uncomment the next line to create a notification subscription for all managed
// accounts.
// .setAllManagedAccounts(true)
.
setTargetAccount
(
parent
)
.
setCallBackUri
(
callbackUri
)
.
setRegisteredEvent
(
eventType
)
.
build
();
// Create the request object.
CreateNotificationSubscriptionRequest
request
=
CreateNotificationSubscriptionRequest
.
newBuilder
()
.
setParent
(
parent
)
.
setNotificationSubscription
(
notificationSubscription
)
.
build
();
System
.
out
.
println
(
"Sending create Notification Subscription request:"
);
// Make the API call.
NotificationSubscription
response
=
notificationsApiServiceClient
.
createNotificationSubscription
(
request
);
System
.
out
.
println
(
"Created 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
();
createNotificationSubscription
(
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\CreateNotificationSubscriptionRequest;
use Google\Shopping\Merchant\Notifications\V1\NotificationSubscription;
use Google\Shopping\Merchant\Notifications\V1\NotificationSubscription\NotificationEventType;
/**
* Creates a Notification Subscription.
*/
class CreateNotificationSubscription
{
/**
* Helper function to construct the parent resource name.
*
* @param string $accountId
* The Merchant Center account ID.
* @return string
* The parent resource name.
*/
private static function getParent($accountId)
{
return "accounts/" . $accountId;
}
/**
* Creates a Notification Subscription for a given Merchant Center account.
*
* @param array $config
* Configuration data for authentication and account details.
* @param string $callbackUri
* The URI to send notifications to.
* @param int $eventType
* The type of event to subscribe to.
* @throws ApiException
*/
public static function createNotificationSubscriptionSample($config, $callbackUri, $eventType): 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 NotificationSubscription object.
$notificationSubscription = new NotificationSubscription([
// Uncomment to apply to all managed accounts.
// 'all_managed_accounts' => true,
'target_account' => $parent,
'call_back_uri' => $callbackUri,
'registered_event' => $eventType,
]);
// Create the request object.
$request = new CreateNotificationSubscriptionRequest([
'parent' => $parent,
'notification_subscription' => $notificationSubscription,
]);
print "Sending create Notification Subscription request:\n";
// Make the API call.
try {
$response = $notificationsApiServiceClient->createNotificationSubscription($request);
print "Created 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 callback URI.
$callbackUri = "https://www.samplesite.com";
// Set the event type.
$eventType = NotificationEventType::PRODUCT_STATUS_CHANGE;
self::createNotificationSubscriptionSample($config, $callbackUri, $eventType);
}
}
// Run the script.
$sample = new CreateNotificationSubscription();
$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 create NotificationSubscription."""
from
examples.authentication
import
configuration
from
examples.authentication
import
generate_user_credentials
from
google.shopping.merchant_notifications_v1
import
CreateNotificationSubscriptionRequest
from
google.shopping.merchant_notifications_v1
import
NotificationsApiServiceClient
from
google.shopping.merchant_notifications_v1
import
NotificationSubscription
_ACCOUNT
=
configuration
.
Configuration
()
.
read_merchant_info
()
_PARENT
=
f
"accounts/
{
_ACCOUNT
}
"
def
create_notification_subscription
():
"""Creates a Notification Subscription for a given Merchant Center account."""
# Gets OAuth Credentials.
credentials
=
generate_user_credentials
.
main
()
# Creates a client.
client
=
NotificationsApiServiceClient
(
credentials
=
credentials
)
# The callback URI to be used to push the notification to.
callback_uri
=
"https://www.samplesite.com"
# The event type to register for notifications.
event_type
=
(
NotificationSubscription
.
NotificationEventType
.
PRODUCT_STATUS_CHANGE
)
# Creates the NotificationSubscription object.
notification_subscription
=
NotificationSubscription
()
# Uncomment the next line to create a notification subscription for all
# managed accounts.
# notification_subscription.all_managed_accounts = True
notification_subscription
.
target_account
=
_PARENT
notification_subscription
.
call_back_uri
=
callback_uri
notification_subscription
.
registered_event
=
event_type
# Creates the request object.
request
=
CreateNotificationSubscriptionRequest
(
parent
=
_PARENT
,
notification_subscription
=
notification_subscription
)
print
(
"Sending create Notification Subscription request:"
)
# Makes the API call.
try
:
response
=
client
.
create_notification_subscription
(
request
=
request
)
print
(
"Created Notification Subscription below:"
)
print
(
response
)
except
RuntimeError
as
e
:
print
(
e
)
if
__name__
==
"__main__"
:
create_notification_subscription
()