Merchant API code sample to create a omnichannel setting.
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.accounts.omnichannelsettings.v1;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.shopping.merchant.accounts.v1.AccountName;
import com.google.shopping.merchant.accounts.v1.CreateOmnichannelSettingRequest;
import com.google.shopping.merchant.accounts.v1.InStock;
import com.google.shopping.merchant.accounts.v1.OmnichannelSetting;
import com.google.shopping.merchant.accounts.v1.OmnichannelSetting.LsfType;
import com.google.shopping.merchant.accounts.v1.OmnichannelSettingsServiceClient;
import com.google.shopping.merchant.accounts.v1.OmnichannelSettingsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to create an omnichannel setting for a given Merchant Center account
* in a given country
*/
public class CreateOmnichannelSettingSample {
public static void createOmnichannelSetting(Config config, String regionCode) throws Exception {
// Obtains OAuth token based on the user's configuration.
GoogleCredentials credential = new Authenticator().authenticate();
// Creates service settings using the credentials retrieved above.
OmnichannelSettingsServiceSettings omnichannelSettingsServiceSettings =
OmnichannelSettingsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Calls the API and catches and prints any network failures/errors.
try (OmnichannelSettingsServiceClient omnichannelSettingsServiceClient =
OmnichannelSettingsServiceClient.create(omnichannelSettingsServiceSettings)) {
String accountId = config.getAccountId().toString();
String parent = AccountName.newBuilder().setAccount(accountId).build().toString();
// Creates an omnichannel setting with GHLSF type in the given country.
CreateOmnichannelSettingRequest request =
CreateOmnichannelSettingRequest.newBuilder()
.setParent(parent)
.setOmnichannelSetting(
OmnichannelSetting.newBuilder()
.setRegionCode(regionCode)
.setLsfType(LsfType.GHLSF)
.setInStock(InStock.getDefaultInstance())
.build())
.build();
System.out.println("Sending create omnichannel setting request:");
OmnichannelSetting response =
omnichannelSettingsServiceClient.createOmnichannelSetting(request);
System.out.println("Inserted Omnichannel Setting below:");
System.out.println(response);
} catch (Exception e) {
System.out.println("An error has occured: ");
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
// The country which you're targeting at.
String regionCode = "{REGION_CODE}";
createOmnichannelSetting(config, regionCode);
}
}
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\Accounts\V1\Client\OmnichannelSettingsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\CreateOmnichannelSettingRequest;
use Google\Shopping\Merchant\Accounts\V1\InStock;
use Google\Shopping\Merchant\Accounts\V1\OmnichannelSetting;
use Google\Shopping\Merchant\Accounts\V1\OmnichannelSetting\LsfType;
/**
* This class demonstrates how to create an omnichannel setting for a given
* Merchant Center account in a given country.
*/
class CreateOmnichannelSetting
{
/**
* Helper to create the parent string.
*
* @param string $accountId The merchant account ID.
* @return string The parent string in the format `accounts/{account}`.
*/
private static function getParent(string $accountId): string
{
return sprintf('accounts/%s', $accountId);
}
/**
* Creates an omnichannel setting for a given Merchant Center account.
*
* @param array $config The configuration file for authentication.
* @param string $regionCode The country for the omnichannel setting.
*/
public static function createOmnichannelSettingSample(
array $config,
string $regionCode
): void {
// Obtains OAuth credentials from the configuration file.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates a client.
$omnichannelSettingsServiceClient = new OmnichannelSettingsServiceClient([
'credentials' => $credentials
]);
// Constructs the parent resource name.
$parent = self::getParent($config['accountId']);
// Creates the omnichannel setting with GHLSF type in the given country.
$omnichannelSetting = new OmnichannelSetting([
'region_code' => $regionCode,
'lsf_type' => LsfType::GHLSF,
'in_stock' => new InStock()
]);
// Creates the request.
$request = new CreateOmnichannelSettingRequest([
'parent' => $parent,
'omnichannel_setting' => $omnichannelSetting
]);
// Calls the API and prints the response.
try {
printf("Sending create omnichannel setting request:%s", PHP_EOL);
$response = $omnichannelSettingsServiceClient->createOmnichannelSetting($request);
printf("Inserted Omnichannel Setting below:%s", PHP_EOL);
print $response->serializeToJsonString(true) . PHP_EOL;
} catch (ApiException $e) {
printf("An error has occured: %s", $e->getMessage());
}
}
/**
* Executes the sample.
*/
public function callSample(): void
{
$config = Config::generateConfig();
// The country which you're targeting.
$regionCode = '{REGION_CODE}';
self::createOmnichannelSettingSample($config, $regionCode);
}
}
// Runs the script.
$sample = new CreateOmnichannelSetting();
$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.
"""This class demonstrates how to create an omnichannel setting."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import CreateOmnichannelSettingRequest
from google.shopping.merchant_accounts_v1 import InStock
from google.shopping.merchant_accounts_v1 import OmnichannelSetting
from google.shopping.merchant_accounts_v1 import OmnichannelSettingsServiceClient
def create_omnichannel_setting(account_id: str, region_code: str) -> None:
"""Creates an omnichannel setting for a given Merchant Center account.
Args:
account_id: The ID of the Merchant Center account.
region_code: The country for which you're creating the setting.
"""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = OmnichannelSettingsServiceClient(credentials=credentials)
# The parent account under which to create the setting.
parent = f"accounts/{account_id}"
# Creates an omnichannel setting with GHLSF type in the given country.
omnichannel_setting = OmnichannelSetting()
omnichannel_setting.region_code = region_code
omnichannel_setting.lsf_type = OmnichannelSetting.LsfType.GHLSF
omnichannel_setting.in_stock = InStock()
# Creates the request.
request = CreateOmnichannelSettingRequest(
parent=parent, omnichannel_setting=omnichannel_setting
)
# Makes the request and catches and prints any error messages.
try:
print("Sending create omnichannel setting request:")
response = client.create_omnichannel_setting(request=request)
print("Inserted Omnichannel Setting below:")
print(response)
except RuntimeError as e:
print("An error has occured: ")
print(e)
if __name__ == "__main__":
# The ID of the account to get the omnichannel settings for.
_ACCOUNT = configuration.Configuration().read_merchant_info()
# The country which you're targeting.
_REGION_CODE = "{REGION_CODE}"
create_omnichannel_setting(_ACCOUNT, _REGION_CODE)