After 1 year of development and more than 700 commits authored by a hundred contributors across the world, the new major release of API Platform is immediately available for download.
API Platform is a PHP 7 framework dedicated to the creation of modern and powerful web APIs. It is especially adapted to build API-centric information systems relying on hypermedia, Linked Data; and consumed by Single-Page Applications (using Javascript libraries such as React or Angular) and mobile apps.
API Platform 2 has been built with 3 strong opinions in mind:
- Creating an API must be an easy and quick process: any web developper should be able to create a REST API in just a few minutes including CRUD support, data validation, filtering and sorting, autogenerated documentation, OAuth and JWT authentication, CORS and other security HTTP headers, caching…
- Modern open formats must be supported out of the box, without requiring any extra work for the developper: Swagger/OpenAPI, JSON-LD, Hydra, HAL, API Problem (RFC 7807) and Schema.org are supported out of the box, the powerful abstraction layer of the framework easily allows adding support for other emerging API formats (JSONAPI and GraphQL support is in progress)
- Every feature of the framework must be extensible, overridable and modular
API Platform v2 is a massive rewrite of the framework, with tons of new features and bug fixes. The whole design has been rethinked, but let’s take a tour of the major new features and changes:
Revamped Config and Metadata: Exposing an API is a Matter of Seconds
Thanks to the new configuration system and the new metadata component, to create a high grade hypermedia API you just have to modelize your data model as a set of PHP classes then to add some annotations. Example:
<?php // src/AppBundle/Entity/Book.php namespace AppBundle\Entity; use ApiPlatform\Core\Annotation\ApiResource; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * A book. * * @ApiResource * @ORM\Entity */ class Book { /** * @var int The id of this book. * * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") */ public $id; /** * @var string|null The ISBN number if this book (or null if doesn't have one). * * @ORM\Column(nullable=true) * @Assert\Isbn */ public $isbn; /** * @var string The title of this book. * * @ORM\Column * @Assert\NotBlank */ public $title; // Prefer private properties, accessors and mutators in real applications }
This single class is enough to get a working API with the following features:
- basic CRUD operations
- data validation and error serialization
- JSON-LD, Swagger, Hydra support (a lot of other formats can easily be added, see below)
- pagination
- an awesome UI and an extensive human-readable documentation reusing PHPDoc’s data and PHP metadata like types (see below)
Checkout our demo to play with a more advanced example (source code, only 2 classes)!
Hypermedia relations are handled out of the box. Leveraging any other feature of API Platform is just a matter of adding a few lines of configuration. Learn more in our getting started guide.
If you don’t like annotations, you can use XML or YAML configuration files instead. If you don’t like the Doctrine ORM (or doesn’t want to tight the exposed data model to the internal model of the database); or if you don’t want to use the Symfony validator, you can create adapters to hook your custom code and use your preferred libraries. API Platform is designed to be fully extensible.
Docker Integration
API Platform’s official distribution is shipped with a Docker setup adapted to API devlopment. It includes a Apache + PHP 7 and a MySQL image. To get an API Platform app up and running on your computer, type the following commands in the main directory:
$ docker-compose up -d # Download, build and run Docker images $ docker-compose exec web bin/console doctrine:schema:create # Create the MySQL schema, only required one time
The app is up and running, browse http://localhost to get started.
API Platform’s images can be deployed easily in production using Docker Swarm (Amazon Web Services, Azure…) or Google Container Engine (with Kubernetes).
A Data Model Generator on Steroid
Instead of crafting your own data model, why not reusing an open vocabulary like the very popular Schema.org and feel the power of Linked Data and Semantic Web? Yes, just like Google but for free.
Since its first release, API Platform comes with a handy code generator allowing to bootstrap a whole PHP data model including classes, properties, getters and setters, full PHPDoc, Doctrine mappings, API Platform’s external vocabulary mappings and validation annotations.
This generator has been updated to match the new configuration format of API Platform 2 and to allow generating custom classes and properties.
The following config file contains a selection of Schema.org’s types and properties. When using the generator to create the corresponding data model, a working API is created without writing a single line of PHP:
types: Book: parent: false # Generate only one class, not the full hierarchy from Schema.org properties: isbn: ~ name: ~ description: ~ author: { range: Text } dateCreated: ~ Review: parent: false properties: reviewBody: ~ rating: { range: Integer, nullable: false } # This is a custom field that doesn't exist in the vocab itemReviewed: { range: Book, nullable: false, cardinality: '(*..1)' }
Learn more about the generator in the docs and the demo application.
Content Negotiation and Built-in Support for JSON-LD, Hydra, HAL, YAML, CSV and XML
API Platform now natively supports content negotiation (only JSON-LD and Hydra was previously supported) as well as most popular API formats. To be able to retrieve or send resources in a specifc format, you enable them in the configuration file:
# app/config/config.yml api_platform: formats: jsonld: ['application/ld+json'] jsonhal: ['application/hal+json'] xml: ['application/xml', 'text/xml'] json: ['application/json'] yaml: ['application/x-yaml'] csv: ['text/csv'] html: ['text/html'] # This is the API Platform UI
This config enable all built-in formats (Symfony 3.2, actually in RC stage, is required for YAML and CSV support). Then, you can request the format you want through the UI using the proper Accept HTTP header, or by adding the format name as extension of any URL of the API (example: https://demo.api-platform.com/books.jsonld).
Support for formats not supported by default can be added by writing custom adapters.
Learn more about content negotiation in API Platform.
A Powerful UI and Automatic Swagger 2 Docs
API Platform 2 generates an extensive Swagger 2/OpenAPI documentation. All URLs and types are automatically described thanks to our powerful metadata extraction system.
A web interface built on top of Swagger UI is also automatically available. Request any API’s URL using a web browser and (thanks to the Accept header sent by the browser), API Platform will display the request sent to the API and the received response in a nice web interface. It will also display a human-readable documentation of the current operation.
Browse the homepage to see the documentation of all available operations, including the description of resources and properties extracted from PHP metadata. Use the sandbox to play with your API.
New Filters and Extension Mechanism
Several new built-in filters have been added. In addition to the existing search, date and range filters, the following are now available:
The filters are now available directly from the UI and documented in both Swagger and Hydra formats.
Learn how to add filters to your API collections.
Filters are now implemented using the brand new extension system. This system allows to hook to the database query generation process and to customize them. It’s particularly useful to implement security features.
Secure by default, tested against OWASP’s recommendations for REST APIs
API Platform 2 follows OWASP’s security recommendations for all its built-in features. We created a test suite to ensure that all recommendations applying to API Platform are followed and documented.
Checkout what API Platform 2 do to secure your API.
Improved performance
We are continuously improving the performance of API Platform and Symfony components it uses (like the Serializer or the PropertyAccess component). This new version is faster than v1 and automatically optimizes SQL queries regarding current serialization groups.
API Platform 2 is also compatible with PHP PM. When using it, API response times are divided by 10.
Availability as Standalone Components, Decoupled from Symfony and Doctrine
API Platform is designed as a set of standalone PHP components: the metadata system; JSON-LD, Hydra, Swagger, HAL serializers, Doctrine and Symfony components bridges…
All those components can be used separately to create your own APIs. For now, the Core library must be downloaded, but a subtree split to allow specific component installation will be available for the 2.1 version. Specific classes can already be used separately of the standard distribution, and without Symfony.
We also moved the code generic enough directly to Symfony. For instance the new Symfony’s PropertyInfo component has been extracted from API Platform. Some new bug fixes and new features and like the MaxDepth as well as YAML and CSV support for the Symfony Serializer has been done while working on API Platform.
Doctrine has never been mandatory to use API Platform, but the set of interfaces to implement to use another persistence system has been rethought and is now documented.
Quality and QA improvements
We dramatically improved the quality of the API Platform code base for this v2. API Platform v1 was already well tested through Behat. In v2 we added a lot of unit tests to prevent bugs and prove that every class respect SOLID principles. The code coverage is now of 96%. Our test suite is automatically run both on Linux (using Travis) and Windows (using AppVeyor).
We also used Scrutinizr and SensioLabs Insight to detect bad practices and improve the overall quality of our code base. API Platform is rated 8.7/10 on Scrutinizr and has the Platinum medal (best rating) on Insight.
Documentation Rewrite and New Website
The documentation has been improved, almost all new features documented and the Getting Started guide fully rewrote. A new website built with React and Redux has also been created. It supports universal rendering and provides a powerful search engine thanks to Algolia’s DocSearch.
A Growing Community
API Platform it’s more than 100 contributors, an awesome core team (Amrouche Hamza, Antoine Bluchet, Samuel ROZE, Teoh Han Hui, Théo FIDRY, Vincent CHALAMON and myself), workshops and conferences talks across the world (don’t miss the workshop at the Berlin’s Symfony Con next week).
API Platform has been in the top GitHub trending PHP repositories several times during past months (and 1st one time) beside great projects like Laravel, Symfony and WordPress and has now more than 1k stars.
It’s amazing! Thanks to everybody having worked on the code base, contributed to the documentation or evangelized about the solution, you rock!
Training, development, professional services and workshops are also provided worldwide by Les-Tilleuls.coop, API Platform’s creators.
What’s next
The release of API Platform v2 is just the first step! We’re already working on new features and some of them are already ready to be merged in the 2.1 branch including:
- Native MongoDB support
- JSONAPI support
- An autogenerated administration using React and Admin On Rest
More to come! Stay tuned!
If you haven’t already done it, it’s time to give a try to API Platform!
Hi there! First of all congrats for this amazing work you are doing! I have tried to install api-platform using composer but it seems that is still on version 1 using that method. Are you gonna change it soon?
Hi @Pedro, installing API Platform through Composer is supported too, the documentation has been updated recently to explain how to do that: https://api-platform.com/docs/distribution/#via-composer
Best regards,