Using API

Since Sylius 1.8, we have offered a new API based on ApiPlatform. Below are examples of how to use the API for basic shop operations. Public API documentation is available here.

Register a customer

To register a new customer, send a single POST request:

curl -X 'POST' \
  'https://v2.demo.sylius.com/api/v2/shop/customers' \
  -H 'accept: application/ld+json' \
  -H 'Content-Type: application/ld+json' \
  -d '{
        "firstName": "shop",
        "lastName": "user",
        "email": "[email protected]",
        "password": "pa$$word",
        "subscribedToNewsletter": true
}'

If the response status is 204, the customer was registered successfully.

Log in to the shop

After registering a customer, you can log in to obtain an authentication token, which is required to access more shop endpoints.

If successful, the response will have a 200 status code and include the token and customer IRI:

Use the token to authenticate subsequent API requests:

Basic Operations: Products, Carts, and Orders

Once the customer is authorized, you can start interacting with products, carts, and orders via the API. Below are the typical operations:

Adding a product to the cart

Create a Cart:

You can create a cart for a logged-in customer by sending a POST request:

Response status 201 will include cart details and the cart's tokenValue, which is needed for subsequent operations.

Add a Product to the Cart:

First, retrieve a product variant by sending a GET request:

Use the @id of the desired variant, and add it to the cart:

The response status 200 confirms the product has been added to the cart.

Changing the Product Quantity in the Cart

To change the quantity of a product already added to the cart, use the following PATCH request:

The response status 200 confirms the quantity change.

Completing the Order

Once the cart is filled, follow these steps to complete the order:

1. Add Customer Address

Add the customer's billing and shipping address by sending a PATCH request:

If no shippingAddress is provided, the billingAddress will be used for both.

2. Select Shipping and Payment Methods

First, get the available shipping and payment methods:

Use the methods' @id in the next steps.

For Shipping:

For Payment:

3. Complete the Order

Finally, complete the order by sending the following request:

The response status 200 confirms the order completion, with the checkoutState changed to completed.

Final Output

The full checkout process has now been completed using the Sylius API. With this API, you can create a fully functional shop frontend based on Sylius' backend logic.

Last updated

Was this helpful?