Create a primary product data source

Merchant API code sample to create a primary product data source.

Java

  // Copyright 2024 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.datasources.v1 
 ; 
 import 
  
 com.google.api.gax.core.FixedCredentialsProvider 
 ; 
 import 
  
 com.google.auth.oauth2.GoogleCredentials 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.CreateDataSourceRequest 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.DataSource 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.PrimaryProductDataSource 
 ; 
 import 
  
 com.google.shopping.type.Destination.DestinationEnum 
 ; 
 import 
  
 shopping.merchant.samples.utils.Authenticator 
 ; 
 import 
  
 shopping.merchant.samples.utils.Config 
 ; 
 /** 
 * This class demonstrates how to create a primary product datasource for the "en" and "GB" 
 * `feedLabel` and `contentLanguage` combination. 
 */ 
 public 
  
 class 
 CreatePrimaryProductDataSourceSample 
  
 { 
  
 private 
  
 static 
  
 String 
  
 getParent 
 ( 
 String 
  
 merchantId 
 ) 
  
 { 
  
 return 
  
 String 
 . 
 format 
 ( 
 "accounts/%s" 
 , 
  
 merchantId 
 ); 
  
 } 
  
 public 
  
 static 
  
 String 
  
 createDataSource 
 ( 
 Config 
  
 config 
 , 
  
 String 
  
 displayName 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 GoogleCredentials 
  
 credential 
  
 = 
  
 new 
  
 Authenticator 
 (). 
 authenticate 
 (); 
  
 DataSourcesServiceSettings 
  
 dataSourcesServiceSettings 
  
 = 
  
 DataSourcesServiceSettings 
 . 
 newBuilder 
 () 
  
 . 
 setCredentialsProvider 
 ( 
 FixedCredentialsProvider 
 . 
 create 
 ( 
 credential 
 )) 
  
 . 
 build 
 (); 
  
 String 
  
 parent 
  
 = 
  
 getParent 
 ( 
 config 
 . 
 getAccountId 
 (). 
 toString 
 ()); 
  
 // The type of data that this datasource will receive. 
  
 PrimaryProductDataSource 
  
 primaryProductDataSource 
  
 = 
  
 PrimaryProductDataSource 
 . 
 newBuilder 
 () 
  
 . 
 addCountries 
 ( 
 "GB" 
 ) 
  
 . 
 setContentLanguage 
 ( 
 "en" 
 ) 
  
 . 
 setFeedLabel 
 ( 
 "GB" 
 ) 
  
 // The destinations do not necessarily have to be explicitly listed in which case the 
  
 // default enabled destinations will be used. 
  
 . 
 addDestinations 
 ( 
  
 PrimaryProductDataSource 
 . 
 Destination 
 . 
 newBuilder 
 () 
  
 . 
 setDestination 
 ( 
 DestinationEnum 
 . 
 SHOPPING_ADS 
 ) 
  
 . 
 setState 
 ( 
 PrimaryProductDataSource 
 . 
 Destination 
 . 
 State 
 . 
 ENABLED 
 ) 
  
 . 
 build 
 ()) 
  
 . 
 addDestinations 
 ( 
  
 PrimaryProductDataSource 
 . 
 Destination 
 . 
 newBuilder 
 () 
  
 . 
 setDestination 
 ( 
 DestinationEnum 
 . 
 FREE_LISTINGS 
 ) 
  
 . 
 setState 
 ( 
 PrimaryProductDataSource 
 . 
 Destination 
 . 
 State 
 . 
 DISABLED 
 ) 
  
 . 
 build 
 ()) 
  
 . 
 build 
 (); 
  
 try 
  
 ( 
 DataSourcesServiceClient 
  
 dataSourcesServiceClient 
  
 = 
  
 DataSourcesServiceClient 
 . 
 create 
 ( 
 dataSourcesServiceSettings 
 )) 
  
 { 
  
 CreateDataSourceRequest 
  
 request 
  
 = 
  
 CreateDataSourceRequest 
 . 
 newBuilder 
 () 
  
 . 
 setParent 
 ( 
 parent 
 ) 
  
 . 
 setDataSource 
 ( 
  
 DataSource 
 . 
 newBuilder 
 () 
  
 . 
 setDisplayName 
 ( 
 displayName 
 ) 
  
 . 
 setPrimaryProductDataSource 
 ( 
 primaryProductDataSource 
 ) 
  
 . 
 build 
 ()) 
  
 . 
 build 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Sending Create PrimaryProduct DataSource request" 
 ); 
  
 DataSource 
  
 response 
  
 = 
  
 dataSourcesServiceClient 
 . 
 createDataSource 
 ( 
 request 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Created DataSource Name below" 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 response 
 . 
 getName 
 ()); 
  
 return 
  
 response 
 . 
 getName 
 (); 
  
 } 
  
 catch 
  
 ( 
 Exception 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 e 
 ); 
  
 System 
 . 
 exit 
 ( 
 1 
 ); 
  
 // Null is necessary to satisfy the compiler as we're not returning a String on failure. 
  
 return 
  
 null 
 ; 
  
 } 
  
 } 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 Config 
  
 config 
  
 = 
  
 Config 
 . 
 load 
 (); 
  
 // The displayed datasource name in the Merchant Center UI. 
  
 String 
  
 displayName 
  
 = 
  
 "British Primary Product Data" 
 ; 
  
 createDataSource 
 ( 
 config 
 , 
  
 displayName 
 ); 
  
 } 
 } 
  
 

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\DataSources\V1\Client\DataSourcesServiceClient; 
 use Google\Shopping\Merchant\DataSources\V1\CreateDataSourceRequest; 
 use Google\Shopping\Merchant\DataSources\V1\DataSource; 
 use Google\Shopping\Merchant\DataSources\V1\PrimaryProductDataSource; 
 use Google\Shopping\Type\Destination\DestinationEnum; 
 /** 
 * This class demonstrates how to create a primary product datasource for the "en" and "GB" 
 * `feedLabel` and `contentLanguage` combination. 
 */ 
 class CreatePrimaryProductDataSourceSample 
 { 
 /** 
 * A helper function to create the parent string for DataSource resources. 
 * 
 * @param string $accountId The Merchant Center account ID. 
 * @return string The parent resource name in the format `accounts/{account_id}`. 
 */ 
 private static function getParent(string $accountId): string 
 { 
 return sprintf("accounts/%s", $accountId); 
 } 
 /** 
 * Creates a new primary product data source. 
 * 
 * @param array $config The configuration array containing the account ID. 
 * @param string $displayName The display name for the new data source. 
 * @return void 
 */ 
 public static function createDataSourceSample(array $config, string $displayName): void 
 { 
 // Gets the OAuth credentials to make the request. 
 $credentials = Authentication::useServiceAccountOrTokenFile(); 
 // Creates options config containing credentials for the client to use. 
 $options = ['credentials' => $credentials]; 
 // Creates a DataSourcesServiceClient. 
 $dataSourcesServiceClient = new DataSourcesServiceClient($options); 
 // Constructs the parent resource name from the account ID. 
 $parent = self::getParent($config['accountId']); 
 // Defines the primary product data source specific settings. 
 $primaryProductDataSource = new PrimaryProductDataSource([ 
 'countries' => ['GB'], 
 'content_language' => 'en', 
 'feed_label' => 'GB', 
 // The destinations do not necessarily have to be explicitly listed in which case the 
 // default enabled destinations will be used. 
 'destinations' => [ 
 new PrimaryProductDataSource\Destination([ 
 'destination' => DestinationEnum::SHOPPING_ADS, 
 'state' => PrimaryProductDataSource\Destination\State::ENABLED 
 ]), 
 new PrimaryProductDataSource\Destination([ 
 'destination' => DestinationEnum::FREE_LISTINGS, 
 'state' => PrimaryProductDataSource\Destination\State::DISABLED 
 ]) 
 ] 
 ]); 
 // Creates the DataSource object. 
 $dataSource = new DataSource([ 
 'display_name' => $displayName, 
 'primary_product_data_source' => $primaryProductDataSource 
 ]); 
 // Prepares the request message to create the data source. 
 $request = new CreateDataSourceRequest([ 
 'parent' => $parent, 
 'data_source' => $dataSource 
 ]); 
 // Calls the API and catches and prints any network failures/errors. 
 try { 
 print "Sending Create PrimaryProduct DataSource request\n"; 
 // Issues the create data source request. 
 $response = $dataSourcesServiceClient->createDataSource($request); 
 print("Created DataSource below\n"); 
 print($response->serializeToJsonString() . PHP_EOL); 
 } catch (ApiException $e) { 
 printf("ApiException was thrown: %s\n", $e->getMessage()); 
 } 
 } 
 /** 
 * Helper to execute the sample. 
 * 
 * @return void 
 */ 
 public function callSample(): void 
 { 
 $config = Config::generateConfig(); 
 // The displayed datasource name in the Merchant Center UI. 
 $displayName = "British Primary Product Data"; 
 self::createDataSourceSample($config, $displayName); 
 } 
 } 
 // Run the script. 
 $sample = new CreatePrimaryProductDataSourceSample(); 
 $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. 
 """Sample for creating a primary product data source.""" 
 from 
  
 examples.authentication 
  
 import 
 configuration 
 from 
  
 examples.authentication 
  
 import 
 generate_user_credentials 
 from 
  
 google.shopping.merchant_datasources_v1 
  
 import 
 CreateDataSourceRequest 
 from 
  
 google.shopping.merchant_datasources_v1 
  
 import 
 DataSource 
 from 
  
 google.shopping.merchant_datasources_v1 
  
 import 
 DataSourcesServiceClient 
 from 
  
 google.shopping.merchant_datasources_v1 
  
 import 
 PrimaryProductDataSource 
 # Used for setting the destination type, e.g., SHOPPING_ADS. 
 from 
  
 google.shopping.type 
  
 import 
 types 
 as 
 merchant_api_types 
 # Fetches the Merchant Center account ID from the configuration file. 
 # This ID is essential for constructing the 'parent' resource path 
 # required by the API. 
 _ACCOUNT_ID 
 = 
 configuration 
 . 
 Configuration 
 () 
 . 
 read_merchant_info 
 () 
 # Constructs the parent resource name format for Data Source operations. 
 _PARENT 
 = 
 f 
 "accounts/ 
 { 
 _ACCOUNT_ID 
 } 
 " 
 def 
  
 create_primary_product_data_source 
 ( 
 display_name 
 : 
 str 
 ) 
 - 
> None 
 : 
  
 """Creates a primary product data source for the 'en' language and 'GB' region, targeting specific destinations. 
 Args: 
 display_name: The user-visible name for the new data source in Merchant 
 Center. 
 Returns: 
 The resource name of the newly created data source if successful, 
 otherwise None. 
 """ 
 # Obtains OAuth 2.0 credentials for API authentication. 
 credentials 
 = 
 generate_user_credentials 
 . 
 main 
 () 
 # Initializes the DataSourcesServiceClient with the obtained credentials. 
 client 
 = 
 DataSourcesServiceClient 
 ( 
 credentials 
 = 
 credentials 
 ) 
 # Configures the PrimaryProductDataSource. 
 # This section defines the core properties of the product data feed. 
 primary_product_data_source 
 = 
 PrimaryProductDataSource 
 () 
 # Sets the target countries for this data source. 
 primary_product_data_source 
 . 
 countries 
 = 
 [ 
 "GB" 
 ] 
 # Sets the content language for the products. 
 primary_product_data_source 
 . 
 content_language 
 = 
 "en" 
 # Sets the feed label, often matching the country or language. 
 primary_product_data_source 
 . 
 feed_label 
 = 
 "GB" 
 # Defines the destinations for this data source and their states. 
 # If destinations are not explicitly listed, defaults will be used. 
 # Configure Shopping Ads destination (enabled). 
 destination_shopping_ads 
 = 
 PrimaryProductDataSource 
 . 
 Destination 
 () 
 destination_shopping_ads 
 . 
 destination 
 = 
 ( 
 merchant_api_types 
 . 
 Destination 
 . 
 DestinationEnum 
 . 
 SHOPPING_ADS 
 ) 
 destination_shopping_ads 
 . 
 state 
 = 
 ( 
 PrimaryProductDataSource 
 . 
 Destination 
 . 
 State 
 . 
 ENABLED 
 ) 
 # Configure Free Listings destination (disabled). 
 destination_free_listings 
 = 
 PrimaryProductDataSource 
 . 
 Destination 
 () 
 destination_free_listings 
 . 
 destination 
 = 
 ( 
 merchant_api_types 
 . 
 Destination 
 . 
 DestinationEnum 
 . 
 FREE_LISTINGS 
 ) 
 destination_free_listings 
 . 
 state 
 = 
 ( 
 PrimaryProductDataSource 
 . 
 Destination 
 . 
 State 
 . 
 DISABLED 
 ) 
 primary_product_data_source 
 . 
 destinations 
 = 
 [ 
 destination_free_listings 
 , 
 destination_shopping_ads 
 ] 
 # Assembles the DataSource object. 
 data_source 
 = 
 DataSource 
 () 
 data_source 
 . 
 display_name 
 = 
 display_name 
 data_source 
 . 
 primary_product_data_source 
 = 
 primary_product_data_source 
 # Prepares the CreateDataSourceRequest. 
 # This request includes the parent account and the data source configuration. 
 request 
 = 
 CreateDataSourceRequest 
 ( 
 parent 
 = 
 _PARENT 
 , 
 data_source 
 = 
 data_source 
 ) 
 try 
 : 
 # Executes the API call to create the data source. 
 print 
 ( 
 "Sending Create PrimaryProduct DataSource request" 
 ) 
 response 
 = 
 client 
 . 
 create_data_source 
 ( 
 request 
 = 
 request 
 ) 
 # Confirms creation and prints the new data source's name. 
 print 
 ( 
 "Created DataSource Name below" 
 ) 
 print 
 ( 
 response 
 ) 
 except 
 RuntimeError 
 as 
 e 
 : 
 # Handles any errors encountered during the API request. 
 print 
 ( 
 e 
 ) 
 if 
 __name__ 
 == 
 "__main__" 
 : 
 # Sets the desired display name for the data source in Merchant Center. 
 datasource_display_name 
 = 
 "British Primary Product Data" 
 # Calls the function to create the data source. 
 create_primary_product_data_source 
 ( 
 datasource_display_name 
 ) 
  
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: