With this post we want to introduce Fusio 2.0 which is a modern PHP based API management and platform which helps to build and manage APIs.
To better understand the evolution of Fusio, I would like take a look at the history of PHP. This starts at roughly the early 2000 years where many CMS/Blog projects like WordPress, Drupal, Typo3 emerged. This was the time where the main problem was to serve dynamic HTML content for the web. From there frameworks like Symfony/Laravel evolved to build more generic web apps which are not targeted at content management. At the heart those frameworks are designed to build web apps, this means they have many components to solve those problems i.e. a template engine to render HTML, a component to process forms, a way to save this data through some ORM at the database, etc.
In the last years we have started to enter the time of API driven apps. This means the target of an web app is not longer only the browser which renders HTML/JS to a user, but instead the app provides an API which can be used by another machine. The problems of such an API driven app are different then for a traditional web apps. Because of this we have developed Fusio, a tool specifically designed to build API based applications.
This means Fusio tries to solves all common API related problems for your so that you can concentrate on building the actual business logic of your app. To be more specific Fusio provides the following features:
- Automatic OpenAPI specification generation
- OAuth2 authorization
- Handling Rate-Limits
- JSON validation and transformation
- Automatic SDK generation
- Event pub/sub support to build async APIs
- Monetization to charge for API calls
- Developer portal where external developers can register
To give you an overview what Fusio provides we take a look at the most common use cases:
Fusio can be used as gateway to your internal API and microservices.
Fusio allows you to build API endpoints without coding knowledge.
Fusio can be used as API framework to build complete customized API endpoints.
To get started please head to Github and download the latest release of Fusio. Then you need to move the folder into the document root of your web server. To install Fusio you can use either the web based installer (located at public/install.php) or you can use the CLI installer. Therefor you need to enter the database credentials to the configuration and run the install command:
php bin/fusio install
In this post we use examples from the headless CMS sample app which you can also find at GitHub.
To start building an API you have to define at first a route. A route is a path i.e. “/foo/bar” or “/foo/:year” which redirects an incoming request to the fitting action. Every route has a corresponding YAML config file which describes the route:
- version: The version describes the version of this route. If the API evolves you can define multiple versions for the same route. A user can then invoke a specific version of the endpoint.
- scopes: Assigns the route to the defined scopes. Scopes are used to give users only access to specific endpoints. In this example a user needs to have access to the “comment” scope to post a new comment.
- methods: This lists all available request methods. The endpoint returns a 405 Method Not Allowed in case the method is not available.
- public: Indicates whether the endpoint is public or private. In case the endpoint is private a user needs to provide an access token (which also has the fitting scopes assigned) to access the method.
- description: A general description what this method does. It is also used in various API specification formats i.e. OpenAPI to describe the API more in detail.
- request: Contains a model which describes the request payload. The action receives the model containing the JSON data.
- responses: Contains a model for each response code which describes the response body format. Should contain for each possible status code a fitting schema or at least for the success response.
- action: The action describes a class, script or url which is called on request. The action executes then the endpoint logic and produces the fitting response.
To work with fully typed models Fusio uses TypeSchema. Through TypeSchema we can automatically generate model classes which can be used as request and response class. In TypeSchema we describe a type through a JSON specification, the following shows an example schema:
Through this we can automatically generate the fitting model classes. Please take a look at the repository to see some generated models.
An action is the basic building block in Fusio and triggers your business logic. It is like a controller in a classical framework, you can also think of it like a serverless lambda function, which can be executed on a route call or via RPC. Every action is independently from each other, this means if one action has a bad performance or is badly written it does not affect the rest of your app. Through this design it is also easy possible to extract your business logic inside an action into a micro-service.
The following action shows a simple action to create a new todo entry on request. In this case the action contains the complete business logic but it is also possible to move this logic into a separate service, therefor please take a look at the sample repository.