Link LFP provider

Merchant API code sample to link 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);
  }
}

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)