Get a Customer

Learn how to fetch a Customer with the commercetools SDKs.

  • After completing this page, you should be able to:

    • Identify the correct process for retrieving a Customer with the commercetools SDKs.
  • Fetching a Customer is pretty straightforward. To retrieve a resource in Composable Commerce, you must provide either the id or key as the identifier of the resource. For Customers, you can find information for either of these two approaches under Get Customer.

    After we fetch the Customer, we receive a Customer response object which contains the fields as described in the Customer representation.

    The docs representation page shows what possible fields exist for Customer.

    A successful response will contain the requested data body in JSON format:

    {
    "id": "cfab735c-2693-4a27-980c-921686c6750b",
    "version": 1,
    "createdAt": "2024-01-16T06:00:31.099Z",
    "lastModifiedAt": "2024-01-16T06:00:31.099Z",
    "lastModifiedBy": {
    "clientId": "VdkVvQ_2M82WsxJFU3A8A6Fz",
    "isPlatformClient": false
    },
    "createdBy": {
    "clientId": "VdkVvQ_2M82WsxJFU3A8A6Fz",
    "isPlatformClient": false
    },
    "email": "example-customer@example.com",
    "firstName": "John",
    "lastName": "Doe",
    "password": "**removed from output**",
    "addresses": [
    {
    "id": "yUmV0kHa",
    "firstName": "John",
    "lastName": "Doe",
    "country": "US",
    "key": "john-doe-example-home"
    }
    ],
    "shippingAddressIds": [],
    "billingAddressIds": [],
    "isEmailVerified": false,
    "key": "john-doe-example",
    "stores": [],
    "authenticationMode": "Password"
    }

    TypeScript SDK

    Create a file called customerFetchByKey.js and copy the following code:

    import { apiRoot } from '../impl/apiClient.js'; // Update to map to your API root
    const customerKey = 'abc';
    async function customerFetchById(customerKey) {
    try {
    const response = await apiRoot
    .customers()
    .withKey({
    key: customerKey,
    })
    .get()
    .execute();
    console.log('Success', JSON.stringify(response.body, null, 2));
    } catch (error) {
    console.log(JSON.stringify(error, null, 2));
    }
    }
    customerFetchById(customerKey);

    Java SDK

    In the following code we have combined the code snippets from the service and exercise class. Make sure you place the code into the correct files and add import statements as needed.

    CustomerService:
    public CompletableFuture<ApiHttpResponse<Customer>> getCustomerByKey(String customerKey) {
    return
    apiRoot
    .customers()
    .withKey(customerKey)
    .get()
    .execute();
    }
    CustomerExercise:
    logger.info("Customer fetch: " +
    customerService
    .getCustomerByKey("john-doe-example")
    .toCompletableFuture().get()
    .getBody().getEmail()
    );

    We trust that by now you can run the above code and fetch the Customer from your Project. If you’re still having trouble, you can review the relevant section in the Prepare your work environment module where we execute a similar fetch request.

    Test your knowledge