Try our example SDK code.
Example SDK code on this page assumes you have set up your SDK as described in the get started guide of your respective SDK. You may need to modify some code if your environment differs.
HTTP API
Manage Customers
Create a Customer
email
and password
.id
.// Create a CustomerDraft with the required fields (email address and password)
CustomerDraft newCustomerDetails = CustomerDraft
.builder()
.email("sdk@example.com")
.password("password")
.build();
// Post the CustomerDraft and get the new Customer
Customer customer = apiRoot
.customers()
.post(newCustomerDetails)
.executeBlocking()
.getBody()
.getCustomer();
// Output the Customer ID
String customerID = customer.getId();
System.out.println(customerID);
{customerID}
is a placeholder in the following examples.Query a Customer
id
after the Customers endpoint. This example code outputs the Customer's email
, but you can access every field within the Customer.// Query a Customer by their ID
Customer queryCustomer = apiRoot
.customers()
.withId("{customerID}")
.get()
.executeBlocking()
.getBody();
// Output the Customer's email address
String customerEmail = queryCustomer.getEmail();
System.out.println(customerEmail);
id
or key
that does not exist, the API returns a Not Found error. This may cause your program to crash.Add a Customer name
CustomerUpdate
, which contains the current version of the Customer and an array/collection of update actions, to the specified Customer.// Create the CustomerUpdate with the current version of the Customer and add the actions
CustomerUpdate customerUpdate = CustomerUpdate
.builder()
// The version of a new Customer is 1. This value increments every time the API applies an update action to the Customer. If the specified version does not match the current version, the request returns an error.
.version(1l)
.plusActions(actionBuilder ->
actionBuilder.setFirstNameBuilder().firstName("John")
)
.plusActions(actionBuilder ->
actionBuilder.setLastNameBuilder().lastName("Smith")
)
.build();
// Post the CustomerUpdate and return the updated Customer
Customer updatedCustomer = apiRoot
.customers()
.withId("{customerID}")
.post(customerUpdate)
.executeBlocking()
.getBody();
// Output the updated Customer's full name
String updatedCustomerName =
updatedCustomer.getFirstName() + " " + updatedCustomer.getLastName();
System.out.println(updatedCustomerName);
Find a Customer by their email address
email
of a Customer is unique, it is a dependable way to find a Customer.where
parameter and a Query Predicate.results
array. If a Customer exists with the email, results
will contain one entry and this Customer's id
is output to the console.// Search for Customers whose email address matches the predicate variable
CustomerPagedQueryResponse customerToFind = apiRoot
.customers()
.get()
.withWhere("email = :customerEmail", "customerEmail", "sdk@example.com")
.executeBlocking()
.getBody();
// As email addresses must be unique, this returns either 0 or 1 Customers.
// If 0, then no Customer exists with this email address.
if (customerToFind.getCount() == 0) {
System.out.println("This email address is not registered.");
} else {
// Since there can be only one Customer resource in the result, it must be the first entry of the results array. This outputs the Customer's id.
String customerID = customerToFind.getResults().get(0).getId();
System.out.println(customerID);
}
Manage Products
Create a ProductType
name
and description
.id
.// Create a ProductTypeDraft with the required fields (name and description)
ProductTypeDraft newProductTypeDetails = ProductTypeDraft
.builder()
.name("The name of your ProductType")
.description("The description of your ProductType")
.build();
// Post the ProductTypeDraft and get the new ProductType
ProductType productType = apiRoot
.productTypes()
.post(newProductTypeDetails)
.executeBlocking()
.getBody();
// Output the ProductType ID
String productTypeID = productType.getId();
System.out.println(productTypeID);
{productTypeID}
is a placeholder in the following examples.Create a Product
name
, productType
, and slug
.id
.// Create a ProductDraft with the required fields (name, productType, and slug)
ProductDraft newProductDetails = ProductDraft
.builder()
.name(stringBuilder ->
stringBuilder
.addValue("en", "English name for your Product")
.addValue("de", "German name for your Product")
)
.productType(typeBuilder -> typeBuilder.id({productTypeID}))
.slug(stringBuilder ->
stringBuilder
.addValue("en", "human-readable-url-for-english-product")
.addValue("de", "human-readable-url-for-german-product")
)
.build();
// Post the ProductDraft and get the new Product
Product product = apiRoot
.products()
.post(newProductDetails)
.executeBlocking()
.getBody();
// Output the Product ID
String productID = product.getId();
System.out.println(productID);
{productID}
is a placeholder in the following examples.Query your Product
id
after the Product endpoint. This example code outputs the Product's version
, but you can access every field within the Product.// Query a Product by its ID
Product queryProduct = apiRoot
.products()
.withId("{productID}")
.get()
.executeBlocking()
.getBody();
// Output the Product's version
System.out.println(queryProduct.getVersion());
Add a Product key
Keys are user-defined unique identifiers that make it easier to manage various resources in your Project. By assigning keys to Products, you can query and update Products using a human-friendly identifier instead of a randomly generated ID. You can also potentially use keys to sync your Products with their identifiers in external databases.
ProductUpdate
, which contains the current version of the Product and an array/collection of update actions, to the specified Product.key
to a Product requires the Set Key update action.// Create the ProductUpdate with the current version of the Product and the update actions
ProductUpdate productUpdate = ProductUpdate
.builder()
// The version of a new Product is 1. This value increments every time the API applies an update action to the Product. If the specified version does not match the current version, the request returns an error.
.version(1l)
.plusActions(actionBuilder ->
actionBuilder.setKeyBuilder().key("new-product-key")
)
.build();
// Post the ProductUpdate and return the updated Product
Product updatedProduct = apiRoot
.products()
.withId("{productID}")
.post(productUpdate)
.executeBlocking()
.getBody();
// Output the updated Product's key
String updatedProductKey = updatedProduct.getKey();
System.out.println(updatedProductKey);
Return a Product by its Key
You can now query the Product by including its key after the Products endpoint. This example code outputs the Product's current English name.
Product findProductByKey = apiRoot
.products()
.withKey("{productKey}")
.get()
.executeBlocking()
.getBody();
// Output the Product's current English name
String productName = findProductByKey
.getMasterData()
.getCurrent()
.getName()
.get("en");
System.out.println(productName);
Import API
Create an Import Container
key
. You can also specify the optional resourceType
field to restrict what can be imported using this Import Container. This example sets resourceType
to category
, which means only CategoryImportRequests can be imported using this Import Container.// Create the ImportContainerDraft
ImportContainerDraft importContainerDraft = ImportContainerDraftBuilder.of()
.key("sdk-example-import-container")
.resourceType(ImportResourceType.CATEGORY)
.build();
// Example call to create an ImportContainer
ImportContainer createImportContainer = importApiRoot
.importContainers()
.post(importContainerDraft)
.executeBlocking()
.getBody();
// Output the response to the console
System.out.println(createImportContainer);
Import a Category
// Create a CategoryImport
CategoryImport categoryImport = CategoryImport.builder()
.key("sdk-example-category-import")
.name(LocalizedString.builder().addValue("en", "SDK Example Category Import").build())
.slug(LocalizedString.builder().addValue("en", "sdk-example-category-import").build())
.build();
// Create a CategoryImportRequest
CategoryImportRequest categoryImportRequest = CategoryImportRequest.builder()
.resources(List.of(categoryImport))
.build();
// Example call to import Categories
ImportResponse createCategoryImport = importApiRoot
.categories()
.importContainers()
.withImportContainerKeyValue("sdk-example-import-container")
.post(categoryImportRequest)
.executeBlocking()
.getBody();
// Output the response to the console
System.out.println(createCategoryImport);
Query the import process
Query Import Operations in the Import Container
// Example call to query Import Operations
ImportOperationPagedResponse queryImportOperations = importApiRoot
.importContainers()
.withImportContainerKeyValue("sdk-example-import-container")
.importOperations()
.get()
.executeBlocking()
.getBody();
// Output the response to the console
System.out.println(queryImportOperations);
Get an Import Summary
// Example call to get an Import Summary for an Import Container
ImportSummary getImportSummary = importApiRoot
.importContainers()
.withImportContainerKeyValue("sdk-example-import-container")
.importSummaries()
.get()
.executeBlocking()
.getBody();
// Output the response to the console
System.out.println(getImportSummary);
Checkout API
Create a Transaction
Ensure you use TransactionDraft from the Checkout SDK package and not TransactionDraft from the HTTP API package.
// Create and post a TransactionDraft to generate a Transaction
Transaction transaction = checkoutApiRoot.with()
.transactions()
.post(TransactionDraft.builder()
.key("a-transaction-key")
.application(a -> a.key("your-application-key"))
.cart(CartResourceIdentifierBuilder.of().key("your-cart-key").build())
.plusTransactionItems(t -> t.amount(a -> a.centAmount(1000).currencyCode("EUR"))
.paymentIntegration(p -> p.key("your-payment-integration-key")))
.build()
)
.executeBlocking()
.getBody();
// Output the created Transaction
System.out.println(transaction);