List data sources

  • This page provides code samples in Java, PHP, and Python for listing data sources within a Merchant Center account.

  • The code samples demonstrate how to use the Merchant API to retrieve all data sources associated with a specific account, including primary and supplemental types.

  • The examples include the necessary steps for authentication and making API calls, along with error handling for network failures.

  • Each sample showcases how to iterate through the API response, extract individual data source details, and filter them based on their type.

  • The code samples also have links to the source files on the Google Merchant API samples GitHub repository, making it easy for users to access and download the code.

Merchant API code sample to list data sources.

AppsScript

  // 
  
 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 
 . 
 /** 
  
 * 
  
 Lists 
  
 all 
  
 data 
  
 sources 
  
 for 
  
 a 
  
 given 
  
 Merchant 
  
 Center 
  
 account 
 . 
  
 */ 
 function 
  
 listDataSources 
 () 
  
 { 
  
 // 
  
 IMPORTANT 
 : 
  
 // 
  
 Enable 
  
 the 
  
 Merchant 
  
 API 
  
 DataSources 
  
 sub 
 - 
 API 
  
 Advanced 
  
 Service 
  
 and 
  
 call 
  
 it 
  
 // 
  
 "MerchantApiDataSources" 
  
 // 
  
 Replace 
  
 this 
  
 with 
  
 your 
  
 Merchant 
  
 Center 
  
 ID 
 . 
  
 const 
  
 accountId 
  
 = 
  
 '<MERCHANT_CENTER_ID>' 
 ; 
  
 // 
  
 Construct 
  
 the 
  
 parent 
  
 name 
  
 const 
  
 parent 
  
 = 
  
 'accounts/' 
  
 + 
  
 accountId 
 ; 
  
 let 
  
 dataSources 
  
 = 
  
 []; 
  
 let 
  
 primaryDataSources 
  
 = 
  
 []; 
  
 try 
  
 { 
  
 console 
 . 
 log 
 ( 
 'Sending list DataSources request' 
 ); 
  
 let 
  
 pageToken 
 ; 
  
 let 
  
 pageSize 
  
 = 
  
 10 
 ; 
  
 // 
  
 Call 
  
 the 
  
 DataSources 
 . 
 list 
  
 API 
  
 method 
 . 
  
 Use 
  
 the 
  
 pageToken 
  
 to 
  
 iterate 
  
 through 
  
 // 
  
 all 
  
 pages 
  
 of 
  
 results 
 . 
  
 do 
  
 { 
  
 response 
  
 = 
  
 MerchantApiDataSources 
 . 
 Accounts 
 . 
 DataSources 
 . 
 list 
 ( 
 parent 
 , 
  
 { 
 pageSize 
 , 
  
 pageToken 
 }); 
  
 for 
  
 ( 
 const 
  
 datasource 
  
 of 
  
 response 
 . 
 dataSources 
 ) 
  
 { 
  
 dataSources 
 . 
 push 
 ( 
 datasource 
 ); 
  
 if 
  
 ( 
 datasource 
 . 
 primaryProductDataSource 
 ) 
  
 { 
  
 primaryDataSources 
 . 
 push 
 ( 
 datasource 
 ); 
  
 } 
  
 } 
  
 pageToken 
  
 = 
  
 response 
 . 
 nextPageToken 
 ; 
  
 } 
  
 while 
  
 ( 
 pageToken 
 ); 
  
 // 
  
 Exits 
  
 when 
  
 there 
  
 is 
  
 no 
  
 next 
  
 page 
  
 token 
 . 
  
 console 
 . 
 log 
 ( 
 'Retrieved ' 
  
 + 
  
 dataSources 
 . 
 length 
  
 + 
  
 ' data sources.' 
 ); 
  
 console 
 . 
 log 
 ( 
  
 'There were ' 
  
 + 
  
 primaryDataSources 
 . 
 length 
  
 + 
  
 ' primary product data sources.' 
 ); 
  
 } 
  
 catch 
  
 ( 
 e 
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
 'ERROR!' 
 ); 
  
 console 
 . 
 log 
 ( 
 e 
 ); 
  
 } 
 } 
  
 

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.DataSource 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.DataSourcesServiceClient.ListDataSourcesPagedResponse 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.DataSourcesServiceSettings 
 ; 
 import 
  
 com.google.shopping.merchant.datasources.v1.ListDataSourcesRequest 
 ; 
 import 
  
 java.util.ArrayList 
 ; 
 import 
  
 shopping.merchant.samples.utils.Authenticator 
 ; 
 import 
  
 shopping.merchant.samples.utils.Config 
 ; 
 /** This class demonstrates how to list all the datasources for a given Merchant Center account */ 
 public 
  
 class 
 ListDataSourcesSample 
  
 { 
  
 private 
  
 static 
  
 String 
  
 getParent 
 ( 
 String 
  
 accountId 
 ) 
  
 { 
  
 return 
  
 String 
 . 
 format 
 ( 
 "accounts/%s" 
 , 
  
 accountId 
 ); 
  
 } 
  
 public 
  
 static 
  
 ArrayList<DataSource> 
  
 listDataSources 
 ( 
 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. 
  
 DataSourcesServiceSettings 
  
 dataSourcesServiceSettings 
  
 = 
  
 DataSourcesServiceSettings 
 . 
 newBuilder 
 () 
  
 . 
 setCredentialsProvider 
 ( 
 FixedCredentialsProvider 
 . 
 create 
 ( 
 credential 
 )) 
  
 . 
 build 
 (); 
  
 // Creates parent to identify the account from which to list all the datasources. 
  
 String 
  
 parent 
  
 = 
  
 getParent 
 ( 
 config 
 . 
 getAccountId 
 (). 
 toString 
 ()); 
  
 // Calls the API and catches and prints any network failures/errors. 
  
 try 
  
 ( 
 DataSourcesServiceClient 
  
 dataSourcesServiceClient 
  
 = 
  
 DataSourcesServiceClient 
 . 
 create 
 ( 
 dataSourcesServiceSettings 
 )) 
  
 { 
  
 // The parent has the format: accounts/{account} 
  
 ListDataSourcesRequest 
  
 request 
  
 = 
  
 ListDataSourcesRequest 
 . 
 newBuilder 
 (). 
 setParent 
 ( 
 parent 
 ). 
 build 
 (); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Sending list datasources request:" 
 ); 
  
 ListDataSourcesPagedResponse 
  
 response 
  
 = 
  
 dataSourcesServiceClient 
 . 
 listDataSources 
 ( 
 request 
 ); 
  
 int 
  
 count 
  
 = 
  
 0 
 ; 
  
 ArrayList<DataSource> 
  
 dataSources 
  
 = 
  
 new 
  
 ArrayList<DataSource> 
 (); 
  
 ArrayList<DataSource> 
  
 justPrimaryDataSources 
  
 = 
  
 new 
  
 ArrayList<DataSource> 
 (); 
  
 // Iterates over all rows in all pages and prints the datasource in each row. 
  
 // Automatically uses the `nextPageToken` if returned to fetch all pages of data. 
  
 for 
  
 ( 
 DataSource 
  
 element 
  
 : 
  
 response 
 . 
 iterateAll 
 ()) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 element 
 ); 
  
 count 
 ++ 
 ; 
  
 dataSources 
 . 
 add 
 ( 
 element 
 ); 
  
 // The below lines show how to filter datasources based on type. 
  
 // `element.hasSupplementalProductDataSource()` would give you supplemental 
  
 // datasources, etc. 
  
 if 
  
 ( 
 element 
 . 
 hasPrimaryProductDataSource 
 ()) 
  
 { 
  
 justPrimaryDataSources 
 . 
 add 
 ( 
 element 
 ); 
  
 } 
  
 } 
  
 System 
 . 
 out 
 . 
 print 
 ( 
 "The following count of elements were returned: " 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 count 
 ); 
  
 return 
  
 dataSources 
 ; 
  
 } 
  
 catch 
  
 ( 
 Exception 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 e 
 ); 
  
 System 
 . 
 exit 
 ( 
 1 
 ); 
  
 return 
  
 null 
 ; 
  
 // Necessary to satisfy the compiler as we're not returning an 
  
 // ArrayList<DataSource> on failure. 
  
 } 
  
 } 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 Config 
  
 config 
  
 = 
  
 Config 
 . 
 load 
 (); 
  
 listDataSources 
 ( 
 config 
 ); 
  
 } 
 } 
  
 

Node.js

  // 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. 
 'use strict' 
 ; 
 const 
  
 fs 
  
 = 
  
 require 
 ( 
 'fs' 
 ); 
 const 
  
 authUtils 
  
 = 
  
 require 
 ( 
 '../../authentication/authenticate.js' 
 ); 
  
 // Assuming auth utils are in this relative path 
 const 
  
 { 
 DataSourcesServiceClient 
 } 
  
 = 
  
 require 
 ( 
 '@google-shopping/datasources' 
 ). 
 v1 
 ; 
  
 // Use the correct client and version 
 /** 
 * This function lists all data sources for a given Merchant Center account. 
 */ 
 async 
  
 function 
  
 listDataSourcesWrapper 
 () 
  
 { 
  
 // Retrieve configuration settings, including the path to the merchant info file. 
  
 const 
  
 config 
  
 = 
  
 authUtils 
 . 
 getConfig 
 (); 
  
 // Read the merchant ID from the specified JSON file. 
  
 const 
  
 merchantInfo 
  
 = 
  
 JSON 
 . 
 parse 
 ( 
 fs 
 . 
 readFileSync 
 ( 
 config 
 . 
 merchantInfoFile 
 , 
  
 'utf8' 
 )); 
  
 const 
  
 merchantId 
  
 = 
  
 merchantInfo 
 . 
 merchantId 
 ; 
  
 // Obtain authenticated credentials for the API call. 
  
 const 
  
 authClient 
  
 = 
  
 await 
  
 authUtils 
 . 
 getOrGenerateUserCredentials 
 (); 
  
 // Configure the client options, including the authentication client. 
  
 const 
  
 clientOptions 
  
 = 
  
 { 
  
 authClient 
 : 
  
 authClient 
 , 
  
 }; 
  
 // Create a new instance of the DataSourcesServiceClient using the options. 
  
 const 
  
 dataSourceClient 
  
 = 
  
 new 
  
 DataSourcesServiceClient 
 ( 
 clientOptions 
 ); 
  
 await 
  
 callListDataSources 
 ( 
 dataSourceClient 
 , 
  
 merchantId 
 ); 
 } 
 /** 
 * Calls the listDataSources API and processes the response. 
 * @param {!DataSourcesServiceClient} dataSourceClient The authenticated client. 
 * @param {string} merchantId The Merchant Center account ID. 
 */ 
 async 
  
 function 
  
 callListDataSources 
 ( 
 dataSourceClient 
 , 
  
 merchantId 
 ) 
  
 { 
  
 // Construct the parent resource name string required by the API. 
  
 // Format: accounts/{merchantId} 
  
 const 
  
 parent 
  
 = 
  
 `accounts/ 
 ${ 
 merchantId 
 } 
 ` 
 ; 
  
 // Prepare the request object for the listDataSources API call. 
  
 const 
  
 request 
  
 = 
  
 { 
  
 parent 
 : 
  
 parent 
 , 
  
 }; 
  
 console 
 . 
 log 
 ( 
 'Sending list data sources request:' 
 ); 
  
 try 
  
 { 
  
 // Call the API to list data sources. This returns an async iterable. 
  
 const 
  
 iterable 
  
 = 
  
 dataSourceClient 
 . 
 listDataSourcesAsync 
 ( 
 request 
 ); 
  
 let 
  
 count 
  
 = 
  
 0 
 ; 
  
 const 
  
 allDataSources 
  
 = 
  
 []; 
  
 const 
  
 primaryDataSources 
  
 = 
  
 []; 
  
 // Iterate asynchronously over all data sources returned by the API. 
  
 // The client library handles pagination automatically. 
  
 for 
  
 await 
  
 ( 
 const 
  
 dataSource 
  
 of 
  
 iterable 
 ) 
  
 { 
  
 console 
 . 
 log 
 ( 
 JSON 
 . 
 stringify 
 ( 
 dataSource 
 , 
  
 null 
 , 
  
 2 
 )); 
  
 // Print the full data source object 
  
 count 
 ++ 
 ; 
  
 allDataSources 
 . 
 push 
 ( 
 dataSource 
 ); 
  
 // Check if the data source is a primary product data source. 
  
 // In Node.js protobufs, check for the presence of the specific oneof field. 
  
 if 
  
 ( 
 dataSource 
 . 
 primaryProductDataSource 
 ) 
  
 { 
  
 primaryDataSources 
 . 
 push 
 ( 
 dataSource 
 ); 
  
 } 
  
 } 
  
 // Print the total number of data sources found. 
  
 console 
 . 
 log 
 ( 
 `The following count of elements were returned: 
 ${ 
 count 
 } 
 ` 
 ); 
  
 // You can optionally log the filtered primary data sources 
  
 // console.log('Primary Product Data Sources:', primaryDataSources); 
  
 } 
  
 catch 
  
 ( 
 error 
 ) 
  
 { 
  
 console 
 . 
 error 
 ( 
 `Failed to list data sources: 
 ${ 
 error 
 . 
 message 
 } 
 ` 
 ); 
  
 } 
 } 
 // Execute the main wrapper function and handle potential promise rejection. 
 listDataSourcesWrapper 
 (). 
 catch 
 ( 
 error 
  
 = 
>  
 { 
  
 console 
 . 
 error 
 ( 
 error 
 . 
 message 
 ); 
  
 process 
 . 
 exitCode 
  
 = 
  
 1 
 ; 
  
 }); 
  
 

PHP

 < ?php 
 /** 
 * 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. 
 */ 
 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\DataSource; 
 use Google\Shopping\Merchant\DataSources\V1\ListDataSourcesRequest; 
 /** 
 * Class to demonstrate listing all the datasources for a given Merchant Center 
 * account. 
 */ 
 class ListDataSourcesSample 
 { 
 /** 
 * Lists all DataSources for the given Merchant Center account. 
 * 
 * @param int $merchantId The Merchant Center Account ID. 
 * @return array An array of DataSources. 
 */ 
 public function listDataSources(int $merchantId): array 
 { 
 // 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 client. 
 $dataSourcesServiceClient = new DataSourcesServiceClient($options); 
 $parent = sprintf('accounts/%s', $merchantId); 
 // Creates the request. 
 $request = (new ListDataSourcesRequest()) 
 ->setParent($parent); 
 print('Sending list datasources request:' . PHP_EOL); 
 // Calls the API and catches and prints any network failures/errors. 
 try { 
 $response = $dataSourcesServiceClient->listDataSources($request); 
 $dataSources = []; 
 $justPrimaryDataSources = []; 
 /** @var DataSource $element */ 
 foreach ($response as $element) { 
 print($element->serializeToJsonString() . PHP_EOL); 
 $dataSources[] = $element; 
 // The below lines show how to filter datasources based on type. 
 // `element.hasSupplementalProductDataSource()` would give you supplemental 
 // datasources, etc. 
 if ($element->hasPrimaryProductDataSource()) { 
 $justPrimaryDataSources[] = $element; 
 } 
 } 
 print('The following count of datasources were returned: ' . count($dataSources) . PHP_EOL); 
 print('... of which are primary datasources: ' . count($justPrimaryDataSources) . PHP_EOL); 
 return $dataSources; 
 } catch (ApiException $ex) { 
 print('Call failed with message: ' . $ex->getMessage() . PHP_EOL); 
 return []; 
 } 
 } 
 // Helper to execute the sample. 
 public function callSample(): void 
 { 
 $config = Config::generateConfig(); 
 // The Merchant Center Account ID. 
 $merchantId = $config['accountId']; 
 self::listDataSources($merchantId); 
 } 
 } 
 $sample = new ListDataSourcesSample(); 
 $sample->callSample();  
 
 

Python

  # -*- coding: utf-8 -*- 
 # 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 
 # 
 #     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 DataSources.""" 
 from 
  
 examples.authentication 
  
 import 
 configuration 
 from 
  
 examples.authentication 
  
 import 
 generate_user_credentials 
 from 
  
 google.shopping 
  
 import 
 merchant_datasources_v1 
 _ACCOUNT 
 = 
 configuration 
 . 
 Configuration 
 () 
 . 
 read_merchant_info 
 () 
 _PARENT 
 = 
 f 
 "accounts/ 
 { 
 _ACCOUNT 
 } 
 " 
 def 
  
 list_data_sources 
 (): 
  
 """Lists the `DataSource` resources for a given account.""" 
 # Gets OAuth Credentials. 
 credentials 
 = 
 generate_user_credentials 
 . 
 main 
 () 
 # Creates a client. 
 client 
 = 
 merchant_datasources_v1 
 . 
 DataSourcesServiceClient 
 ( 
 credentials 
 = 
 credentials 
 ) 
 # Creates the request. 
 request 
 = 
 merchant_datasources_v1 
 . 
 ListDataSourcesRequest 
 ( 
 parent 
 = 
 _PARENT 
 ) 
 # Makes the request and catch and print any error messages. 
 try 
 : 
 response 
 = 
 client 
 . 
 list_data_sources 
 ( 
 request 
 = 
 request 
 ) 
 primary_data_sources 
 = 
 [] 
 for 
 data_source 
 in 
 response 
 . 
 data_sources 
 : 
 # PrimaryProductDataSource is a oneOf field. If it is set, then this is a 
 # primary data source. 
 if 
 data_source 
 . 
 primary_product_data_source 
 : 
 primary_data_sources 
 . 
 append 
 ( 
 data_source 
 ) 
 print 
 ( 
 f 
 "List request successful. You have 
 { 
 len 
 ( 
 response 
 . 
 data_sources 
 ) 
 } 
 total" 
 f 
 " data sources, of which 
 { 
 len 
 ( 
 primary_data_sources 
 ) 
 } 
 are primary data" 
 " sources" 
 ) 
 except 
 RuntimeError 
 as 
 e 
 : 
 print 
 ( 
 "List request failed" 
 ) 
 print 
 ( 
 e 
 ) 
 if 
 __name__ 
 == 
 "__main__" 
 : 
 list_data_sources 
 () 
  
 
Design a Mobile Site
View Site in Mobile | Classic
Share by: