List programs
Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to list programs.
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.accounts.programs.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.ListProgramsRequest;
import com.google.shopping.merchant.accounts.v1.Program;
import com.google.shopping.merchant.accounts.v1.ProgramsServiceClient;
import com.google.shopping.merchant.accounts.v1.ProgramsServiceClient.ListProgramsPagedResponse;
import com.google.shopping.merchant.accounts.v1.ProgramsServiceSettings;
import shopping.merchant.samples.utils.Authenticator;
import shopping.merchant.samples.utils.Config;
/**
* This class demonstrates how to list all shopping program resources for a Merchant Center account.
*/
public class ListProgramsSample {
public static void listPrograms(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.
ProgramsServiceSettings programsServiceSettings =
ProgramsServiceSettings.newBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(credential))
.build();
// Creates parent to identify the account for which to list programs.
String parent = AccountName.of(config.getAccountId().toString()).toString();
// Calls the API and catches and prints any network failures/errors.
try (ProgramsServiceClient programsServiceClient =
ProgramsServiceClient.create(programsServiceSettings)) {
ListProgramsRequest request = ListProgramsRequest.newBuilder().setParent(parent).build();
System.out.println("Sending List Programs request:");
ListProgramsPagedResponse response = programsServiceClient.listPrograms(request);
int count = 0;
// Iterates over all programs in all pages and prints each program.
// Automatically uses the `nextPageToken`, if returned, to fetch all pages.
for (Program program : response.iterateAll()) {
System.out.println(program);
count++;
}
System.out.print("The count of Programs returned: ");
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) throws Exception {
Config config = Config.load();
listPrograms(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\Accounts\V1\Client\ProgramsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\ListProgramsRequest;
/**
* This class demonstrates how to list all shopping program resources for a Merchant Center account.
*/
class ListProgramsSample
{
/**
* Lists all programs for the given Merchant Center account.
*
* @param array $config The configuration data for authentication and account ID.
* @return void
*/
public static function listPrograms($config): 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 client.
$programsServiceClient = new ProgramsServiceClient($options);
// Creates parent to identify the account for which to list programs.
$parent = "accounts/" . $config['accountId'];
// Calls the API and catches and prints any network failures/errors.
try {
$request = new ListProgramsRequest(['parent' => $parent]);
print "Sending List Programs request:\n";
$response = $programsServiceClient->listPrograms($request);
$count = 0;
// Iterates over all programs in all pages and prints each program.
// Automatically uses the `nextPageToken`, if returned, to fetch all pages.
foreach ($response->iterateAllElements() as $program) {
print_r($program);
$count++;
}
print "The count of Programs returned: ";
print $count . "\n";
} catch (ApiException $e) {
print $e->getMessage();
}
}
/**
* Helper to execute the sample.
*
* @return void
*/
public function callSample(): void
{
$config = Config::generateConfig();
self::listPrograms($config);
}
}
// Run the script
$sample = new ListProgramsSample();
$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 for listing all program resources for a Merchant Center account."""
from examples.authentication import configuration
from examples.authentication import generate_user_credentials
from google.shopping.merchant_accounts_v1 import ListProgramsRequest
from google.shopping.merchant_accounts_v1 import ProgramsServiceClient
_ACCOUNT = configuration.Configuration().read_merchant_info()
def list_programs():
"""Lists all program resources for a Merchant Center account."""
# Gets OAuth Credentials.
credentials = generate_user_credentials.main()
# Creates a client.
client = ProgramsServiceClient(credentials=credentials)
# Creates parent to identify the account for which to list programs.
parent = "accounts/" + _ACCOUNT
# Creates the request.
request = ListProgramsRequest(parent=parent)
# Makes the request and catches and prints any error messages.
try:
print("Sending List Programs request:")
response = client.list_programs(request=request)
count = 0
# Iterates over all programs in all pages and prints each program.
for program in response:
print(program)
count += 1
print("The count of Programs returned: ")
print(count)
except RuntimeError as e:
print(e)
if __name__ == "__main__":
list_programs()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-13 UTC.
[null,null,["Last updated 2025-08-13 UTC."],[[["\u003cp\u003eThis code sample demonstrates how to list all shopping program resources associated with a specific Merchant Center account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe provided examples are available in both Java and Python, offering flexibility for developers working with either language.\u003c/p\u003e\n"],["\u003cp\u003eBoth code samples use OAuth credentials to authenticate and then utilize the \u003ccode\u003eProgramsServiceClient\u003c/code\u003e to interact with the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe core functionality involves creating a \u003ccode\u003eListProgramsRequest\u003c/code\u003e and iterating through the paged response to list all available programs, including the total count of programs returned.\u003c/p\u003e\n"],["\u003cp\u003eThe code uses a "try catch" block in both code samples to properly catch and print any network failures/errors.\u003c/p\u003e\n"]]],["The code samples demonstrate how to list all shopping program resources for a given Merchant Center account using the Merchant API in Java, PHP, and Python. Each script authenticates, creates a client, and builds a `ListProgramsRequest`. This request, specifying the account, is sent to the API. The response iterates through all programs, printing each program and a final count, handling potential network errors or exceptions during the API call.\n"],null,["# List programs\n\nMerchant API code sample to list programs. \n\n### Java\n\n // Copyright 2024 Google LLC\n //\n // Licensed under the Apache License, Version 2.0 (the \"License\");\n // you may not use this file except in compliance with the License.\n // You may obtain a copy of the License at\n //\n // https://www.apache.org/licenses/LICENSE-2.0\n //\n // Unless required by applicable law or agreed to in writing, software\n // distributed under the License is distributed on an \"AS IS\" BASIS,\n // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n // See the License for the specific language governing permissions and\n // limitations under the License.\n\n package shopping.merchant.samples.accounts.programs.v1;\n import com.google.api.gax.core.FixedCredentialsProvider;\n import com.google.auth.oauth2.GoogleCredentials;\n import com.google.shopping.merchant.accounts.v1.AccountName;\n import com.google.shopping.merchant.accounts.v1.ListProgramsRequest;\n import com.google.shopping.merchant.accounts.v1.Program;\n import com.google.shopping.merchant.accounts.v1.ProgramsServiceClient;\n import com.google.shopping.merchant.accounts.v1.ProgramsServiceClient.ListProgramsPagedResponse;\n import com.google.shopping.merchant.accounts.v1.ProgramsServiceSettings;\n import shopping.merchant.samples.utils.Authenticator;\n import shopping.merchant.samples.utils.Config;\n\n /**\n * This class demonstrates how to list all shopping program resources for a Merchant Center account.\n */\n public class ListProgramsSample {\n\n public static void listPrograms(Config config) throws Exception {\n\n // Obtains OAuth token based on the user's configuration.\n GoogleCredentials credential = new Authenticator().authenticate();\n\n // Creates service settings using the credentials retrieved above.\n ProgramsServiceSettings programsServiceSettings =\n ProgramsServiceSettings.newBuilder()\n .setCredentialsProvider(FixedCredentialsProvider.create(credential))\n .build();\n\n // Creates parent to identify the account for which to list programs.\n String parent = AccountName.of(config.getAccountId().toString()).toString();\n\n // Calls the API and catches and prints any network failures/errors.\n try (ProgramsServiceClient programsServiceClient =\n ProgramsServiceClient.create(programsServiceSettings)) {\n\n ListProgramsRequest request = ListProgramsRequest.newBuilder().setParent(parent).build();\n\n System.out.println(\"Sending List Programs request:\");\n ListProgramsPagedResponse response = programsServiceClient.listPrograms(request);\n\n int count = 0;\n\n // Iterates over all programs in all pages and prints each program.\n // Automatically uses the `nextPageToken`, if returned, to fetch all pages.\n for (Program program : response.iterateAll()) {\n System.out.println(program);\n count++;\n }\n System.out.print(\"The count of Programs returned: \");\n System.out.println(count);\n } catch (Exception e) {\n System.out.println(e);\n }\n }\n\n public static void main(String[] args) throws Exception {\n Config config = Config.load();\n\n listPrograms(config);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/programs/v1/ListProgramsSample.java\n\n### PHP\n\n \u003c?php\n /**\n * Copyright 2025 Google LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n require_once __DIR__ . '/../../../../vendor/autoload.php';\n require_once __DIR__ . '/../../../Authentication/Authentication.php';\n require_once __DIR__ . '/../../../Authentication/Config.php';\n use Google\\ApiCore\\ApiException;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\Client\\ProgramsServiceClient;\n use Google\\Shopping\\Merchant\\Accounts\\V1\\ListProgramsRequest;\n\n /**\n * This class demonstrates how to list all shopping program resources for a Merchant Center account.\n */\n class ListProgramsSample\n {\n /**\n * Lists all programs for the given Merchant Center account.\n *\n * @param array $config The configuration data for authentication and account ID.\n * @return void\n */\n public static function listPrograms($config): void\n {\n // Gets the OAuth credentials to make the request.\n $credentials = Authentication::useServiceAccountOrTokenFile();\n\n // Creates options config containing credentials for the client to use.\n $options = ['credentials' =\u003e $credentials];\n\n // Creates a client.\n $programsServiceClient = new ProgramsServiceClient($options);\n\n // Creates parent to identify the account for which to list programs.\n $parent = \"accounts/\" . $config['accountId'];\n\n // Calls the API and catches and prints any network failures/errors.\n try {\n $request = new ListProgramsRequest(['parent' =\u003e $parent]);\n\n print \"Sending List Programs request:\\n\";\n $response = $programsServiceClient-\u003elistPrograms($request);\n\n $count = 0;\n\n // Iterates over all programs in all pages and prints each program.\n // Automatically uses the `nextPageToken`, if returned, to fetch all pages.\n foreach ($response-\u003eiterateAllElements() as $program) {\n print_r($program);\n $count++;\n }\n print \"The count of Programs returned: \";\n print $count . \"\\n\";\n } catch (ApiException $e) {\n print $e-\u003egetMessage();\n }\n }\n\n /**\n * Helper to execute the sample.\n *\n * @return void\n */\n public function callSample(): void\n {\n $config = Config::generateConfig();\n self::listPrograms($config);\n }\n }\n\n // Run the script\n $sample = new ListProgramsSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/programs/v1/ListProgramsSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\n # Copyright 2024 Google LLC\n #\n # Licensed under the Apache License, Version 2.0 (the \"License\");\n # you may not use this file except in compliance with the License.\n # You may obtain a copy of the License at\n #\n # http://www.apache.org/licenses/LICENSE-2.0\n #\n # Unless required by applicable law or agreed to in writing, software\n # distributed under the License is distributed on an \"AS IS\" BASIS,\n # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n # See the License for the specific language governing permissions and\n # limitations under the License.\n \"\"\"A module for listing all program resources for a Merchant Center account.\"\"\"\n\n from examples.authentication import configuration\n from examples.authentication import generate_user_credentials\n from google.shopping.merchant_accounts_v1 import ListProgramsRequest\n from google.shopping.merchant_accounts_v1 import ProgramsServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def list_programs():\n \"\"\"Lists all program resources for a Merchant Center account.\"\"\"\n\n # Gets OAuth Credentials.\n credentials = generate_user_credentials.main()\n\n # Creates a client.\n client = ProgramsServiceClient(credentials=credentials)\n\n # Creates parent to identify the account for which to list programs.\n parent = \"accounts/\" + _ACCOUNT\n\n # Creates the request.\n request = ListProgramsRequest(parent=parent)\n\n # Makes the request and catches and prints any error messages.\n try:\n print(\"Sending List Programs request:\")\n response = client.list_programs(request=request)\n\n count = 0\n # Iterates over all programs in all pages and prints each program.\n for program in response:\n print(program)\n count += 1\n\n print(\"The count of Programs returned: \")\n print(count)\n\n except RuntimeError as e:\n print(e)\n\n if __name__ == \"__main__\":\n list_programs()\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/programs/v1/list_programs_sample.py"]]