Merchant API code sample to filter accounts.
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
.
/**
*
Filters
and
lists
accounts
for
which
the
logged
-
in
user
has
access
to
*/
function
filterAccounts
()
{
//
IMPORTANT
:
//
Enable
the
Merchant
API
Accounts
sub
-
API
Advanced
Service
and
call
it
//
"MerchantApiAccounts"
//
Create
the
filter
string
.
//
Documentation
can
be
found
at
//
https
:
//
developers
.
google
.
com
/
merchant
/
api
/
guides
/
accounts
/
filter
-
syntax
const
filter
=
'accountName = "*store*" AND relationship(providerId = 123)'
;
try
{
console
.
log
(
'Sending filter Accounts request'
);
let
pageToken
;
let
pageSize
=
500
;
//
Call
the
Accounts
.
list
API
method
with
a
filter
.
Use
the
pageToken
to
iterate
through
//
all
pages
of
results
.
do
{
response
=
MerchantApiAccounts
.
Accounts
.
list
({
pageSize
,
pageToken
,
filter
});
for
(
const
account
of
response
.
accounts
)
{
console
.
log
(
account
);
}
pageToken
=
response
.
nextPageToken
;
}
while
(
pageToken
);
//
Exits
when
there
is
no
next
page
token
.
}
catch
(
e
)
{
console
.
log
(
'ERROR!'
);
console
.
log
(
e
);
}
}
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.accounts.v1
;
import
com.google.api.gax.core.FixedCredentialsProvider
;
import
com.google.auth.oauth2.GoogleCredentials
;
import
com.google.shopping.merchant.accounts.v1.Account
;
import
com.google.shopping.merchant.accounts.v1.AccountsServiceClient
;
import
com.google.shopping.merchant.accounts.v1.AccountsServiceClient.ListAccountsPagedResponse
;
import
com.google.shopping.merchant.accounts.v1.AccountsServiceSettings
;
import
com.google.shopping.merchant.accounts.v1.ListAccountsRequest
;
import
shopping.merchant.samples.utils.Authenticator
;
import
shopping.merchant.samples.utils.Config
;
/** This class demonstrates how to filter the accounts the user making the request has access to. */
public
class
FilterAccountsSample
{
public
static
void
filterAccounts
(
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.
AccountsServiceSettings
accountsServiceSettings
=
AccountsServiceSettings
.
newBuilder
()
.
setCredentialsProvider
(
FixedCredentialsProvider
.
create
(
credential
))
.
build
();
// Calls the API and catches and prints any network failures/errors.
try
(
AccountsServiceClient
accountsServiceClient
=
AccountsServiceClient
.
create
(
accountsServiceSettings
))
{
// Filter for accounts with display names containing "store" and a provider with the ID "123":
String
filter
=
"accountName = \"*store*\" AND relationship(providerId = 123)"
;
// Filter for all subaccounts of account "123":
// String filter2 = "relationship(providerId = 123 AND service(type =
// \"ACCOUNT_AGGREGATION\"))";
// String filter3 = "relationship(service(handshakeState = \"APPROVED\" AND type =
// \"ACCOUNT_MANAGEMENT\") AND providerId = 123)";
ListAccountsRequest
request
=
ListAccountsRequest
.
newBuilder
().
setFilter
(
filter
).
build
();
System
.
out
.
println
(
"Sending list accounts request with filter:"
);
ListAccountsPagedResponse
response
=
accountsServiceClient
.
listAccounts
(
request
);
int
count
=
0
;
// Iterates over all rows in all pages and prints the sub-account
// in each row.
// `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
// request to fetch all pages of data.
for
(
Account
account
:
response
.
iterateAll
())
{
System
.
out
.
println
(
account
);
count
++
;
}
System
.
out
.
print
(
"The following count of elements were returned: "
);
System
.
out
.
println
(
count
);
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
Config
config
=
Config
.
load
();
filterAccounts
(
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\AccountsServiceClient;
use Google\Shopping\Merchant\Accounts\V1\ListAccountsRequest;
/**
* This class demonstrates how to filter the accounts the user making the request has access to.
*/
class FilterAccounts
{
public static function filterAccounts(array $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.
$accountsServiceClient = new AccountsServiceClient($options);
// Calls the API and catches and prints any network failures/errors.
try {
// Filter for accounts with display names containing "store" and a provider with the ID "123":
$filter = "accountName = \"*store*\" AND relationship(providerId = 123)";
// Filter for all subaccounts of account "123":
// $filter = "relationship(providerId = 123 AND service(type = \"ACCOUNT_AGGREGATION\"))";
// $filter = "relationship(service(handshakeState = \"APPROVED\" AND type =
// \"ACCOUNT_MANAGEMENT\") AND providerId = 123)";
$request = new ListAccountsRequest(['filter' => $filter]);
print "Sending list accounts request with filter:\n";
$response = $accountsServiceClient->listAccounts($request);
$count = 0;
// Iterates over all rows in all pages and prints the sub-account
// in each row.
// `response.iterateAll()` automatically uses the `nextPageToken` and recalls the
// request to fetch all pages of data.
foreach ($response->iterateAllElements() as $account) {
print_r($account);
$count++;
}
print "The following count of elements were returned: ";
print $count . PHP_EOL;
} catch (ApiException $e) {
print $e->getMessage();
}
}
public function callSample(): void
{
$config = Config::generateConfig();
self::filterAccounts($config);
}
}
$sample = new FilterAccounts();
$sample->callSample();