Merchant API code sample to link a LFP provider.

### Java

    // 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.

    package shopping.merchant.samples.accounts.lfpproviders.v1;

    import com.google.api.gax.core.FixedCredentialsProvider;
    import com.google.auth.oauth2.GoogleCredentials;
    import com.google.shopping.merchant.accounts.v1.LfpProviderName;
    import com.google.shopping.merchant.accounts.v1.LfpProvidersServiceClient;
    import com.google.shopping.merchant.accounts.v1.LfpProvidersServiceSettings;
    import com.google.shopping.merchant.accounts.v1.LinkLfpProviderRequest;
    import shopping.merchant.samples.utils.Authenticator;
    import shopping.merchant.samples.utils.Config;

    /** This class demonstrates how to link the Lfp Providers for a given Merchant Center account */
    public class LinkLfpProviderSample {

      public static void linkLfpProvider(String lfpProviderName, String externalAccountId)
          throws Exception {

        // Obtains OAuth token based on the user's configuration.
        GoogleCredentials credential = new Authenticator().authenticate();

        // Creates service settings using the credentials retrieved above.
        LfpProvidersServiceSettings lfpProvidersServiceSettings =
            LfpProvidersServiceSettings.newBuilder()
                .setCredentialsProvider(FixedCredentialsProvider.create(credential))
                .build();

        // Calls the API and catches and prints any network failures/errors.
        try (LfpProvidersServiceClient lfpProvidersServiceClient =
            LfpProvidersServiceClient.create(lfpProvidersServiceSettings)) {
          LinkLfpProviderRequest request =
              LinkLfpProviderRequest.newBuilder()
                  .setName(lfpProviderName)
                  .setExternalAccountId(externalAccountId)
                  .build();

          System.out.println("Sending link lfp provider request:");
          // Empty response returned on success.
          lfpProvidersServiceClient.linkLfpProvider(request);
          System.out.println(String.format("Successfully linked to LFP provider: %s", lfpProviderName));
        } 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();

        // Replace with the actual region code you want to use.
        String regionCode = "REGION_CODE"; // e.g., "US"
        String lfpProviderId = "LFP_PROVIDER_ID";
        // The name of the lfp provider you want to link, returned from `lfpProviders.findLfpProviders`.
        // It's of the form
        // "accounts/{account_id}/omnichannelSettings/{omnichannel_settings}/lfpProviders/{lfp_provider}"
        LfpProviderName lfpProviderName =
            LfpProviderName.newBuilder()
                .setAccount(config.getAccountId().toString())
                .setOmnichannelSetting(regionCode)
                .setLfpProvider(lfpProviderId)
                .build();
        // External account id by which this merchant is known to the LFP provider.
        String externalAccountId = String.valueOf(config.getAccountId());

        linkLfpProvider(lfpProviderName.toString(), externalAccountId);
      }
    }

### 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\LfpProvidersServiceClient;
    use Google\Shopping\Merchant\Accounts\V1\LinkLfpProviderRequest;

    /**
     * This class demonstrates how to link an LFP Provider for a given Merchant
     * Center account.
     */
    class LinkLfpProviderSample
    {
        /**
         * A helper function to create the LFP provider name string.
         *
         * @param string $accountId The Merchant Center account ID.
         * @param string $regionCode The region code for the omnichannel setting.
         * @param string $lfpProviderId The ID of the LFP provider.
         *
         * @return string The name has the format:
         * `accounts/{account}/omnichannelSettings/{omnichannelSetting}/lfpProviders/{lfpProvider}`
         */
        private static function getName(
            string $accountId,
            string $regionCode,
            string $lfpProviderId
        ): string {
            return sprintf(
                "accounts/%s/omnichannelSettings/%s/lfpProviders/%s",
                $accountId,
                $regionCode,
                $lfpProviderId
            );
        }

        /**
         * Links an LFP provider to a Merchant Center account.
         *
         * @param string $lfpProviderName The resource name of the LFP provider.
         * @param string $externalAccountId The external account ID with the provider.
         */
        public static function linkLfpProvider(
            string $lfpProviderName,
            string $externalAccountId
        ): 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.
            $lfpProvidersServiceClient = new LfpProvidersServiceClient($options);

            // Creates the request with the LFP provider name and external account ID.
            $request = (new LinkLfpProviderRequest())
                ->setName($lfpProviderName)
                ->setExternalAccountId($externalAccountId);

            // Calls the API and catches and prints any network failures/errors.
            try {
                printf("Sending link lfp provider request:%s", PHP_EOL);
                // The call returns an empty response on success.
                $lfpProvidersServiceClient->linkLfpProvider($request);
                printf(
                    "Successfully linked to LFP provider: %s%s",
                    $lfpProviderName,
                    PHP_EOL
                );
            } catch (ApiException $e) {
                printf("An error has occured: %s%s", PHP_EOL, $e);
            }
        }

        /**
         * Helper to execute the sample.
         */
        public static function callSample(): void
        {
            $config = Config::generateConfig();

            // Replace with the actual region code you want to use.
            $regionCode = 'REGION_CODE'; // e.g., "US"
            $lfpProviderId = 'LFP_PROVIDER_ID';

            // The name of the lfp provider you want to link, returned from
            // `lfpProviders.findLfpProviders`.
            $lfpProviderName = self::getName(
                $config['accountId'],
                $regionCode,
                $lfpProviderId
            );

            // External account id by which this merchant is known to the LFP provider.
            $externalAccountId = $config['accountId'];

            self::linkLfpProvider($lfpProviderName, $externalAccountId);
        }
    }

    // Runs the sample.
    LinkLfpProviderSample::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 linking LFP providers."""
    from examples.authentication import configuration
    from examples.authentication import generate_user_credentials
    from google.shopping.merchant_accounts_v1 import LfpProvidersServiceClient
    from google.shopping.merchant_accounts_v1 import LinkLfpProviderRequest

    # Gets the merchant account ID from the configuration file.
    _ACCOUNT = configuration.Configuration().read_merchant_info()


    def link_lfp_provider(
        lfp_provider_name: str, external_account_id: str
    ) -> None:
      """Links the LFP Providers for a given Merchant Center account."""

      # Gets OAuth Credentials.
      credentials = generate_user_credentials.main()

      # Creates a client.
      client = LfpProvidersServiceClient(credentials=credentials)

      # Creates the request with the provider name and external account ID.
      request = LinkLfpProviderRequest(
          name=lfp_provider_name, external_account_id=external_account_id
      )

      print("Sending link lfp provider request:")
      # Makes the request and catches and prints any error messages.
      try:
        # An empty response is returned on success.
        client.link_lfp_provider(request=request)
        print(f"Successfully linked to LFP provider: {lfp_provider_name}")
      except RuntimeError as e:
        print("An error has occured: ")
        print(e)


    if __name__ == "__main__":
      # The region code for the omnichannel setting.
      _REGION_CODE = "REGION_CODE"  # e.g., "US"
      # The ID of the LFP provider.
      _LFP_PROVIDER_ID = "LFP_PROVIDER_ID"

      # The name of the LFP provider to link, returned from `find_lfp_providers`.
      # Format:
      # `accounts/{account}/omnichannelSettings/{omnichannel_setting}/lfpProviders/{lfp_provider}`
      lfp_provider = f"accounts/{_ACCOUNT}/omnichannelSettings/{_REGION_CODE}/lfpProviders/{_LFP_PROVIDER_ID}"

      # The external account ID by which this merchant is known to the LFP provider.
      _external_account_id = str(_ACCOUNT)

      link_lfp_provider(lfp_provider, _external_account_id)