Stay organized with collections
Save and categorize content based on your preferences.
Merchant API code sample to enable program.
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.packageshopping.merchant.samples.accounts.programs.v1;importcom.google.api.gax.core.FixedCredentialsProvider;importcom.google.auth.oauth2.GoogleCredentials;importcom.google.shopping.merchant.accounts.v1.EnableProgramRequest;importcom.google.shopping.merchant.accounts.v1.Program;importcom.google.shopping.merchant.accounts.v1.ProgramName;importcom.google.shopping.merchant.accounts.v1.ProgramsServiceClient;importcom.google.shopping.merchant.accounts.v1.ProgramsServiceSettings;importshopping.merchant.samples.utils.Authenticator;importshopping.merchant.samples.utils.Config;/** This class demonstrates how to enable a shopping program for a Merchant Center account. */publicclassEnableProgramSample{publicstaticvoidenableProgram(Configconfig,Stringprogram)throwsException{// Obtains OAuth token based on the user's configuration.GoogleCredentialscredential=newAuthenticator().authenticate();// Creates service settings using the credentials retrieved above.ProgramsServiceSettingsprogramsServiceSettings=ProgramsServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credential)).build();// Creates program name to identify the program.Stringname=ProgramName.newBuilder().setAccount(config.getAccountId().toString()).setProgram(program).build().toString();// Calls the API and catches and prints any network failures/errors.try(ProgramsServiceClientprogramsServiceClient=ProgramsServiceClient.create(programsServiceSettings)){// The name has the format: accounts/{account}/programs/{program}EnableProgramRequestrequest=EnableProgramRequest.newBuilder().setName(name).build();System.out.println("Sending Enable Program request:");Programresponse=programsServiceClient.enableProgram(request);System.out.println("Enabled Program below");System.out.println(response);}catch(Exceptione){System.out.println(e);}}publicstaticvoidmain(String[]args)throwsException{Configconfig=Config.load();// Replace this with the name of the program to be enabled.Stringprogram="free-listings";enableProgram(config,program);}}
<?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\EnableProgramRequest;/** * This class demonstrates how to enable a shopping program for a Merchant Center account. */class EnableProgramSample{ /** * Enables a program for the given Merchant Center account. * * @param array $config The configuration data for authentication and account ID. * @param string $program The program to enable. * @return void */ public static function enableProgram($config, $program): 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 program name to identify the program. $name = $parent = "accounts/" . $config['accountId'] . "/programs/" . $program; // Calls the API and catches and prints any network failures/errors. try { // The name has the format: accounts/{account}/programs/{program} $request = new EnableProgramRequest(['name' => $name]); print "Sending Enable Program request:\n"; $response = $programsServiceClient->enableProgram($request); print "Enabled Program below\n"; print_r($response); } catch (ApiException $e) { print $e->getMessage(); } } /** * Helper to execute the sample. * * @return void */ public function callSample(): void { $config = Config::generateConfig(); // Replace this with the name of the program to be enabled. $program = "free-listings"; self::enableProgram($config, $program); }}// Run the script$sample = new EnableProgramSample();$sample->callSample();
# -*- 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."""A module for enabling a program for a Merchant Center account."""fromexamples.authenticationimportconfigurationfromexamples.authenticationimportgenerate_user_credentialsfromgoogle.shopping.merchant_accounts_v1importEnableProgramRequestfromgoogle.shopping.merchant_accounts_v1importProgramsServiceClient_ACCOUNT=configuration.Configuration().read_merchant_info()defenable_program(program):"""Enables a program for the given Merchant Center account."""# Gets OAuth Credentials.credentials=generate_user_credentials.main()# Creates a client.client=ProgramsServiceClient(credentials=credentials)# Creates program name to identify the program.name="accounts/"+_ACCOUNT+"/programs/"+program# Creates the request.request=EnableProgramRequest(name=name)# Makes the request and catches and prints any error messages.try:response=client.enable_program(request=request)print("Enabled Program below")print(response)returnresponseexceptRuntimeErrorase:print(e)returnNoneif__name__=="__main__":# Replace this with the name of the program to be enabled.program_to_enable="free-listings"enable_program(program_to_enable)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-13 UTC."],[[["\u003cp\u003eThis code sample demonstrates how to enable a shopping program for a Merchant Center account using the Merchant API.\u003c/p\u003e\n"],["\u003cp\u003eThe program utilizes the \u003ccode\u003eProgramsServiceClient\u003c/code\u003e to send an \u003ccode\u003eEnableProgramRequest\u003c/code\u003e to the API.\u003c/p\u003e\n"],["\u003cp\u003eThe code configures authentication using OAuth and sets up the \u003ccode\u003eProgramsServiceSettings\u003c/code\u003e with the retrieved credentials.\u003c/p\u003e\n"],["\u003cp\u003eThe program name is constructed using the account ID and program identifier, following the format \u003ccode\u003eaccounts/{account}/programs/{program}\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eenableProgram\u003c/code\u003e function takes the configuration and the program name, to send a request and receive a response that is then outputted.\u003c/p\u003e\n"]]],[],null,["# Enable program\n\nMerchant API code sample to enable program. \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.EnableProgramRequest;\n import com.google.shopping.merchant.accounts.v1.Program;\n import com.google.shopping.merchant.accounts.v1.ProgramName;\n import com.google.shopping.merchant.accounts.v1.ProgramsServiceClient;\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 /** This class demonstrates how to enable a shopping program for a Merchant Center account. */\n public class EnableProgramSample {\n\n public static void enableProgram(Config config, String program) 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 program name to identify the program.\n String name =\n ProgramName.newBuilder()\n .setAccount(config.getAccountId().toString())\n .setProgram(program)\n .build()\n .toString();\n\n // Calls the API and catches and prints any network failures/errors.\n try (ProgramsServiceClient programsServiceClient =\n ProgramsServiceClient.create(programsServiceSettings)) {\n\n // The name has the format: accounts/{account}/programs/{program}\n EnableProgramRequest request = EnableProgramRequest.newBuilder().setName(name).build();\n\n System.out.println(\"Sending Enable Program request:\");\n Program response = programsServiceClient.enableProgram(request);\n\n System.out.println(\"Enabled Program below\");\n System.out.println(response);\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 // Replace this with the name of the program to be enabled.\n String program = \"free-listings\";\n\n enableProgram(config, program);\n }\n } \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/java/src/main/java/shopping/merchant/samples/accounts/programs/v1/EnableProgramSample.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\\EnableProgramRequest;\n\n /**\n * This class demonstrates how to enable a shopping program for a Merchant Center account.\n */\n class EnableProgramSample\n {\n /**\n * Enables a program for the given Merchant Center account.\n *\n * @param array $config The configuration data for authentication and account ID.\n * @param string $program The program to enable.\n * @return void\n */\n public static function enableProgram($config, $program): 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 program name to identify the program.\n $name = $parent = \"accounts/\" . $config['accountId'] . \"/programs/\" . $program;\n\n\n // Calls the API and catches and prints any network failures/errors.\n try {\n // The name has the format: accounts/{account}/programs/{program}\n $request = new EnableProgramRequest(['name' =\u003e $name]);\n\n print \"Sending Enable Program request:\\n\";\n $response = $programsServiceClient-\u003eenableProgram($request);\n\n print \"Enabled Program below\\n\";\n print_r($response);\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\n // Replace this with the name of the program to be enabled.\n $program = \"free-listings\";\n self::enableProgram($config, $program);\n }\n }\n\n // Run the script\n $sample = new EnableProgramSample();\n $sample-\u003ecallSample(); \n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/php/examples/accounts/programs/v1/EnableProgramSample.php\n\n### Python\n\n # -*- coding: utf-8 -*-\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 # 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 enabling a program 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 EnableProgramRequest\n from google.shopping.merchant_accounts_v1 import ProgramsServiceClient\n\n _ACCOUNT = configuration.Configuration().read_merchant_info()\n\n\n def enable_program(program):\n \"\"\"Enables a program for the given 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 program name to identify the program.\n name = \"accounts/\" + _ACCOUNT + \"/programs/\" + program\n\n # Creates the request.\n request = EnableProgramRequest(name=name)\n\n # Makes the request and catches and prints any error messages.\n try:\n response = client.enable_program(request=request)\n print(\"Enabled Program below\")\n print(response)\n return response\n except RuntimeError as e:\n print(e)\n return None\n\n\n if __name__ == \"__main__\":\n # Replace this with the name of the program to be enabled.\n program_to_enable = \"free-listings\"\n enable_program(program_to_enable)\n\n https://github.com/google/merchant-api-samples/blob/c6de994268c785ce22af0065932518a9ac5b3c03/python/examples/accounts/programs/v1/enable_program_sample.py"]]