Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Building linked data APIs with API Platform

Saturday, September 2nd, 2017 by Servage

api-platformAPI Platform is a framework for building RESTful APIs using PHP. It’s built on top of Symfony 3, and creating your first RESTful API can be done in just 5 minutes. API Platform is specifically designed for building APIs that utilize linked data, which is often used by libraries, social network websites, online stores and other websites where data is strongly related to other data.

Features Briefly

API Platform comes with a big collection of features designed to make building APIs using linked data easy. It comes with a CRUD out of the box for create, read, update and delete actions like RESTful APIs usually do. It integrates well with Swagger, a tool to automatically generate documentation from code. It also supports JSON-LD and Hydra, which are the formats used to determine what the responses from the API look like. You can also generate data models, such as books or products, using the open collection of

The 5-Minute Setup

WordPress is not the only tool famous for its 5-minute setup. API Platform is just a simple to set up for the first time. All you need is a basic development environment with PHP 7 and Composer. The framework can be installed by running “composer create-project api-platform/api-platform library-api”. This installs the whole framework and also runs “composer install” for you.

All there is left is to create a database. You can do these by going to the project root directory and running “php bin/console doctrine:database:create” followed by “php bin/console doctrine:schema:create”. If you already have a web server running, you can open the project in your web browser now and you should see the Swagger documentation page. If you don’t have a web server, you can use PHP’s built-in server: “php bin/console server:run” and go to http://localhost.

Inside API Platform

That was simple enough, and without writing a single line of code you should now have a functional API running. But how does this all work?

API Platform uses something called data providers to fetch data from wherever you want, most likely from a relational database. These data providers are the connection between your business logic (the code you write) and your database.

API Platform also has endpoints configured with annotations by default. These endpoints, such as “/books/:id” can be reached by sending HTTP requests to the API. They act like controllers in a typical API. You get access to the dynamic parts of the route (ID in this case). Then you can call a method of the data provider to get data from your database. It usually looks like this: $book = $dataProvider->getItem(‘Book’, $id). You can then return this data from the controller file using the return keyword and API Platform wraps it inside a Hydra-compliant JSON-LD response. If you are not familiar with Hydra or JSON-LD, don’t worry, it’s just JSON after all. And most importantly, API Platform does this all for you automatically.

On a simple level, this should help you get started with your first linked data API. There is more to API Platform for sure, like models and fetching data from external sources such as Elasticsearch or other APIs, but you should now have an idea of what API Platform is capable of if you ever end up building an API using linked data or graph databases.

Building linked data APIs with API Platform, 5.0 out of 5 based on 1 rating
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.