---
title: List Existing Attributes
slug: php-sdk/list-existing-attributes
docTags: 
createdAt: 2024-05-13T16:07:09.881Z
---

:::hint{type="info"}
*This guide assumes you have installed the latest version of `klevu/php-sdk` , either via composer or from source, and autoloading is functioning as expected.*
:::

:::hint{type="info"}
Examples in this guide are for vanilla PHP and, as such, will use `new` to instantiate objects. If you are using a modern framework such as Magento or Symfony, you should utilise your object manager for dependency injection and preferencing of interfaces where possible.
:::

## Retrieve All Attributes for an Account

To retrieve attributes used by Klevu's XML Feed Monitoring service, first locate your Klevu API and REST AUTH keys and create an `AccountCredentials` object (see Quickstart for steps).

Next, instantiate an `Indexing\AttributeService` object and call the `get` method to return a collection of all attributes registered in Klevu indexing.
The `get` method will throw exceptions if any issues arise, so you should catch and handle these appropriately.

At this point, you will have an `AttributeIterator` object containing `Attribute` models for all registered attributes, which can be filtered and used in subsequent operations.

```php
<?php

declare(strict_types=1);

use Klevu\PhpSDK\Exception\Api\BadRequestException;
use Klevu\PhpSDK\Exception\Api\BadResponseException;
use Klevu\PhpSDK\Exception\ValidationException;
use Klevu\PhpSDK\Model\AccountCredentials;
use Klevu\PhpSDK\Service\Indexing\AttributesService;

$accountCredentials = new AccountCredentials(
    jsApiKey: "klevu-123456780",
    restAuthKey: "ABCDE1234567890",
);
$attributesService = new AttributesService(
    // Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
);

try {
    $attributes = $attributesService->get(
        accountCredentials: $accountCredentials,
    );
} catch (ValidationException $exception) {
    // One or more supplied arguments are missing or invalid
} catch (BadRequestException $exception) {
    // Klevu API rejected the request as invalid
} catch (BadResponseException $exception) {
    // Klevu API responded in an unexpected manner
}
```

### Filter Attributes

If you need to filter the return value of `get`, for example to remove immutable attributes or to identify attributes of a specific data type, you can call the `filter` method with your callback.&#x20;

Note: filtering an iterator in this way will return a copy, and will not modify the data contained within the original return object.

```php
<?php

// snip

$customAttributes = $attributes->filter(
    callback: static fn (AttributeInterface $attribute): bool => !$attribute->isImmutable(),
);
```

## Retrieve a Specific Attribute by Name

To retrieve a registered attribute by name, first locate your Klevu API and REST AUTH keys and create an `AccountCredentials` object (see Quickstart for steps).

Next, instantiate an `Indexing\AttributeService` object and call the `getByName` method with your credentials and the name of the attribute being search for.
The `getByName` method will throw exceptions if any issues arise, so you should catch and handle these appropriately.

At this point, you will have either an `Attribute` object or null.

:::hint{type="info"}
Note: this method does not map to a discrete API endpoint. Its purpose is essentially syntactic sugar, wrapping `get` and `filter` calls described above.
:::

:::hint{type="warning"}
The `attributeName` value is not case sensitive in Klevu's indexer and, therefore, is not case sensitive in name lookups in the PHP-SDK.
For example, a request for `example_attribute` may return `EXAMPLE_ATTRIBUTE` or other cased variations; though only one attribute (at most) will ever be returned from the API.

The `attributeName` property will reflect the case used at time of registration.
:::

```php
<?php

declare(strict_types=1);

use Klevu\PhpSDK\Exception\Api\BadRequestException;
use Klevu\PhpSDK\Exception\Api\BadResponseException;
use Klevu\PhpSDK\Exception\ValidationException;
use Klevu\PhpSDK\Model\AccountCredentials;
use Klevu\PhpSDK\Service\Indexing\AttributesService;

$accountCredentials = new AccountCredentials(
    jsApiKey: "klevu-123456780",
    restAuthKey: "ABCDE1234567890",
);
$attributesService = new AttributesService(
    // Inject any relevant dependencies, such as HTTP Client; Logger; or User Agent provider
);

try {
    $attribute = $attributesService->getByName(
        accountCredentials: $accountCredentials,
        attributeName: 'example_attribute',
    );
} catch (ValidationException $exception) {
    // One or more supplied arguments are missing or invalid
} catch (BadRequestException $exception) {
    // Klevu API rejected the request as invalid
} catch (BadResponseException $exception) {
    // Klevu API responded in an unexpected manner
}
```

***

## Source Code Reference

- Api\Model\Indexing\\[AttributeInterface](https://github.com/klevu/php-sdk/blob/master/src/Api/Model/Indexing/AttributeInterface.php)
- Exception\Api\\[BadRequestException](https://github.com/klevu/php-sdk/blob/master/src/Exception/Api/BadRequestException.php)
- Exception\Api\\[BadResponseException](https://github.com/klevu/php-sdk/blob/master/src/Exception/Api/BadResponseException.php)
- Exception\\[ValidationException](https://github.com/klevu/php-sdk/blob/master/src/Exception/ValidationException.php)
- Model\\[AccountCredentials](https://github.com/klevu/php-sdk/blob/master/src/Model/AccountCredentials.php)
- Service\Indexing\\[AttributesService](https://github.com/klevu/php-sdk/blob/master/src/Service/Indexing/AttributesService.php)

