Page Summary
-
This content provides code samples in Java, PHP, and Python demonstrating how to list all products within a Merchant Center account using the Merchant API.
-
Each code sample includes the necessary authentication steps to access the Merchant Center account using OAuth credentials, through service accounts or token files.
-
The code samples in each language create a request object to list the products, send the request to the Products Service, and handle the response by iterating through and printing the products data returned, as well as displaying error messages when they occur.
-
The provided code dynamically determines the account from which the products should be listed from.
-
Each code example also references where the code can be found in the public GitHub repository for the Google merchant API samples.
Merchant API code sample to list products.
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
products
for
a
given
Merchant
Center
account
.
*/
function
productList
()
{
//
IMPORTANT
:
//
Enable
the
Merchant
API
Products
sub
-
API
Advanced
Service
and
call
it
//
"MerchantApiProducts"
//
Replace
this
with
your
Merchant
Center
ID
.
const
accountId
=
'<MERCHANT_CENTER_ID>'
;
//
Construct
the
parent
name
const
parent
=
'accounts/'
+
accountId
;
try
{
console
.
log
(
'Sending list Products request'
);
let
pageToken
;
//
Set
the
page
size
to
1000.
This
is
the
maximum
allowed
page
size
.
let
pageSize
=
1000
;
console
.
log
(
'Retrieved products below:'
);
//
Call
the
Products
.
list
API
method
.
Use
the
pageToken
to
iterate
through
//
all
pages
of
results
.
do
{
response
=
MerchantApiProducts
.
Accounts
.
Products
.
list
(
parent
,
{
pageToken
,
pageSize
});
console
.
log
(
response
);
pageToken
=
response
.
nextPageToken
;
}
while
(
pageToken
);
//
Exits
when
there
is
no
next
page
token
.
}
catch
(
e
)
{
console
.
log
(
'ERROR!'
);
console
.
log
(
e
);
}
}
CSharp
// 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.
using
System
;
using
static
MerchantApi
.
Authenticator
;
using
Google.Api.Gax
;
using
Google.Apis.Auth.OAuth2
;
using
Google.Apis.Auth.OAuth2.Flows
;
using
Google.Apis.Auth.OAuth2.Responses
;
using
Google.Apis.Http
;
using
Newtonsoft.Json
;
using
Google.Shopping.Merchant.Products.V1
;
namespace
MerchantApi
{
public
class
ListProductsSample
{
public
void
ListProducts
(
string
merchantId
)
{
Console
.
WriteLine
(
"================================================================="
);
Console
.
WriteLine
(
"Listing all Products"
);
Console
.
WriteLine
(
"================================================================="
);
// Authenticate using either oAuth or service account
ICredential
auth
=
Authenticator
.
Authenticate
(
MerchantConfig
.
Load
(),
ProductsServiceClient
.
DefaultScopes
[
0
]);
// Create client
ProductsServiceSettings
productsServiceSettings
=
ProductsServiceSettings
.
GetDefault
();
// Create the ProductsServiceClient with the credentials
ProductsServiceClientBuilder
productsServiceClientBuilder
=
new
ProductsServiceClientBuilder
{
Credential
=
auth
};
ProductsServiceClient
client
=
productsServiceClientBuilder
.
Build
();
// Initialize the parent
// The parent has the format: accounts/{account}
AccountName
parent
=
AccountName
.
FromAccount
(
merchantId
);
Console
.
WriteLine
(
$"Listing products for account: {parent}"
);
// Initialize request argument(s)
ListProductsRequest
request
=
new
ListProductsRequest
{
Parent
=
parent
.
ToString
(),
PageSize
=
1000
// Optional: specify the maximum number of products to return per page
};
// List all products in the account
// Note: The API may return fewer than 1000 products
PagedEnumerable<ListProductsResponse
,
Product
>
response
=
client
.
ListProducts
(
request
);
// Print the paginated results. This automatically handles pagination
// and retrieves all products in the account.
foreach
(
ListProductsResponse
page
in
response
.
AsRawResponses
())
{
Console
.
WriteLine
(
"A page of results:"
);
foreach
(
Product
item
in
page
)
{
// Pretty print the products
Console
.
WriteLine
(
JsonConvert
.
SerializeObject
(
item
,
Formatting
.
Indented
));
}
}
}
internal
static
void
Main
(
string
[]
args
)
{
var
config
=
MerchantConfig
.
Load
();
string
merchantId
=
config
.
MerchantId
.
Value
.
ToString
();
var
sample
=
new
ListProductsSample
();
sample
.
ListProducts
(
merchantId
);
}
}
}
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.products.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.products.v1.ListProductsRequest
;
import
com.google.shopping.merchant.products.v1.Product
;
import
com.google.shopping.merchant.products.v1.ProductsServiceClient
;
import
com.google.shopping.merchant.products.v1.ProductsServiceClient.ListProductsPagedResponse
;
import
com.google.shopping.merchant.products.v1.ProductsServiceSettings
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to list all the products for a given merchant center account */
public
class
ListProductsSample
{
private
static
String
getParent
(
String
accountId
)
{
return
String
.
format
(
"accounts/%s"
,
accountId
);
}
public
static
void
listProducts
(
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.
ProductsServiceSettings
productsServiceSettings
=
ProductsServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
// Creates parent to identify the account from which to list all products.
String
parent
=
getParent
(
config
.
getAccountId
().
toString
());
// Calls the API and catches and prints any network failures/errors.
try
(
ProductsServiceClient
productsServiceClient
=
ProductsServiceClient
.
create
(
productsServiceSettings
))
{
// The parent has the format: accounts/{account}
// Page size is set to the maximum value. If you are returned more
// responses than your page size, this code will automatically
// re-call the service with the `pageToken` until all responses
// are returned.
ListProductsRequest
request
=
ListProductsRequest
.
newBuilder
().
setParent
(
parent
).
setPageSize
(
1000
).
build
();
System
.
out
.
println
(
"Sending list products request:"
);
ListProductsPagedResponse
response
=
productsServiceClient
.
listProducts
(
request
);
int
count
=
0
;
// 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
(
Product
product
:
response
.
iterateAll
())
{
System
.
out
.
println
(
product
);
// The product includes the `productStatus` field
// That shows approval and disapproval information.
count
++
;
}
System
.
out
.
print
(
"The following count of products were returned: "
);
System
.
out
.
println
(
count
);
}
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
();
listProducts
(
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'
);
const
{
ProductsServiceClient
}
=
require
(
'@google-shopping/products'
).
v1
;
/**
* Lists all products for a given merchant.
*/
async
function
main
()
{
try
{
const
config
=
authUtils
.
getConfig
();
// Read merchant_id from merchant-info.json
const
merchant_info
=
JSON
.
parse
(
fs
.
readFileSync
(
config
.
merchantInfoFile
,
'utf8'
));
const
merchant_id
=
merchant_info
.
merchantId
;
// Construct parent. Parent is in the format of accounts/{merchant_id}
const
parent
=
'accounts/'
+
merchant_id
;
// Get credentials
const
authClient
=
await
authUtils
.
getOrGenerateUserCredentials
();
// Create options object for the client
const
options
=
{
'authClient'
:
authClient
};
// Create client
const
productsClient
=
new
ProductsServiceClient
(
options
);
// Construct request. Set the page size to the maximum value.
const
request
=
{
parent
:
parent
,
pageSize
:
1000
};
// Run request
const
iterable
=
productsClient
.
listProductsAsync
(
request
);
for
await
(
const
response
of
iterable
)
{
console
.
log
(
response
);
}
}
catch
(
error
)
{
console
.
error
(
error
.
message
);
}
}
main
();
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\ApiCore\PagedListResponse;
use Google\Shopping\Merchant\Products\V1\ListProductsRequest;
use Google\Shopping\Merchant\Products\V1\Product;
use Google\Shopping\Merchant\Products\V1\Client\ProductsServiceClient;
/**
* This class demonstrates how list all products in your Merchant Center account.
*/
class ListProducts
{
/**
* A helper function to create the parent string.
*
* @param array $accountId
* The account that owns the product.
*
* @return string The parent has the format: `accounts/{account_id}`
*/
private static function getParent($accountId)
{
return sprintf("accounts/%s", $accountId);
}
/**
* Lists all products in your Merchant Center account.
*
* @param array $config
* The configuration data used for authentication and getting the acccount
* ID.
*/
public static function listProductsSample($config)
{
// Obtains OAuth token based on the user's configuration.
$credentials = Authentication::useServiceAccountOrTokenFile();
// Creates options config containing credentials for the client to use.
$options = ['credentials' => $credentials];
// Creates a client.
$productsServiceClient = new ProductsServiceClient($options);
// Creates parent to identify the account from which to list all products.
$parent = self::getParent($config['accountId']);
// Creates the request.
// Page size is set to the maximum value. If you are returned more
// responses than your page size, this code will automatically
// re-call the service with the `pageToken` until all responses
// are returned.
$request = new ListProductsRequest(['parent' => $parent, 'page_size' => 1000]);
// Calls the API and catches and prints any network failures/errors.
try {
echo "Sending list products request:\n";
/**
* Call the listProducts service and get back a response object
* with all products.
*
* @var PagedListResponse $response
*/
$response = $productsServiceClient->listProducts($request);
/**
* Loop through every product and print out its data.
*
* @var Product $product
*/
foreach ($response as $product) {
// The product includes the `productStatus` field that shows approval
// and disapproval information.
printf(
'Product data: %s%s',
$product->serializeToJsonString(), PHP_EOL
);
}
} catch (ApiException $e) {
printf('Call failed with message: %s%s', $e->getMessage(), PHP_EOL);
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::listProductsSample($config);
}
}
// Run the script
$sample = new ListProducts();
$sample->callSample();

