How to create custom API for your eCommerce store?

APIs are very important for any online business. With APIs it becomes easier for other businesses or people to provide services on top of your business. If you are having an online business already or envision to have one, your platform must be able to expose and consume APIs.

Why? Because this will make sure that your business is scalable. As you grow, you may wish to use the same website backend services for your mobile application. This will be done when your platform supports API integrations. With a platform like EvenCart, you can get your APIs exposed and running in no time.

How to create API for EvenCart store?

The architecture of EvenCart has been written in such a way that any code for performing some operation, an API endpoint is automatically generated corresponding to that operation. For example, an operation can be as simple as adding a product to the cart.

With conventional systems, each such operation will need to be replicated at two points, one for UI endpoint and one for API endpoint. This simply means that it'll take twice as much time to get your application ready. EvenCart allows to you write operations using API first approach. What that means is that whenever your write an action or operation, you're actually writing it for API.

If the same action is requested for UI rendering, the html of the page will be written. If the same call is made to the URL with /api/ prefix in the request path, the API system kicks in.

EvenCart uses and recommends attribute routing. It exposes two routing attributes namely DualGet and DualPost.

Any action method marked with [DualGet] or [DualPost] attribute will, in addition to an MVC route, will also create an API endpoint attribute. The additional created route uses the same path pattern as specified in the route template of the attribute with an additional /api/ prefixed to the path.

Depending on the presence or absense of this prefix in the requested URL, the platform sends an HTML or a JSON response.

Example:
Consider for example, a simple MVC action for saving a to-do list item. The conventional method looks like this.

[HttpGet("todo-list", Name="TodoItemList")]
public IActionResult TodoList(TodoItemModel todoItem)
{
    var model = _todoListFactory.GetAll();
    return View(model);
}

The action method above will fetch the items and return it to the Html View that'll render the list. If we wish to retrieve the same items via an API, the conventional approach will need an additional method like below.

[HttpGet("api/todo-list", Name="ApiTodoItemList")]
public IActionResult TodoList(TodoItemModel todoItem)
{
    var model = _todoListFactory.GetAll();
    return Json(model);
}

In the code above, any requests made to http://yourdomain.com/todo-list will render the Html page while any request made to http://yourdomain.com/api/todo-list will return a JSON response.

While the approach above will work when we have fewer endpoints, doing similar thing for an application as big as a shopping cart or a social network, can be a tedious and difficult to maintain. The dual attributes simplify this process by creating a single action method to serve both endpoints.

[DualGet("todo-list", Name="TodoItemList")]
public IActionResult TodoList(TodoItemModel todoItem)
{
    var model = _todoListFactory.GetAll();
    return R.Success.With("todoItems", model).Result;
}

The DualGet attribute above will automatically create two similar endpoints as above and return appropriate responses depending on the URL, without duplicating the same code. This makes API creation a breeze with EvenCart.

The customized MVC response variable R allows you to pass multiple models to your views as well as the JSON reponses.

The DualGet attribute is available in EvenCart.Infrastructure package. Any plugin that references this dll will be able to utilize DualRouting.

hAPI Days 🙂