Publishing an API on the WSO2 API Manager is quite straightforward. You define the API using the Publisher app. Here you define the context, resources and HTTP verbs, indicate endpoints and set tiers and throttling to name but a few. But there are alternatives to getting an API to the API Manager. In this blog we will look at the Import / export API’s functionality. In another blog we will look at the swagger definitions as a way to define and publish APIs.
Import / Export API’s
If you want to export or import an API to a deployment of your API Manager, the first thing is to download the war file that takes care of this. This is actually a java application that will copy the API definition including mediation, images, documentation and so on and creates a zip file.
Please note that this file is particular to the API Manager version, so the above link will only work for API Manager 2.1.0. After downloading, the war file needs to be deployed to the API Manager, this can be done by dropping the file in [APIM-HOME]/repository/deployment/server/webapps.
Exporting an API is simple. You need to do a GET to the API that was deployed with the war file.
It uses basic authorization (the YRWR… is base 64 encoded admin:admin credentials). The parameters are in red. Note that this curl command works on a local installation with a port offset of zero.
curl -H “Authorization:Basic YWRtaW46YWRtaW4=” -X GET “https://localhost:9443/api-import-export-2.1.0-v2/export-api?name=[API-NAME]&version=[VERSION]&provider=[WHO PUBLISHED]” -k > [FILENAME].zip
In order to test this, let’s export the Pizzashack API that is the sample that comes with the API Manager.
Clicking the button ‘Deploy Sample API’. The Pizzashack API is now deployed and ready to be used or, in our case to be exported.
We open up a command window or terminal session and give the command below:
curl -H “Authorization:Basic YWRtaW46YWRtaW4=” -X GET “https://localhost:9443/api-import-export-2.1.0-v2/export-api?name=PizzaShackAPI&version=1.0.0&provider=admin” -k > ExportAPI.zip
We end up with a zip file:
10/06/2017 12:54 PM 150,294 ExportAPI.zip
Import an API
The same tool, but a different API, can be used to import an API to the API Manager. In this case we are going to import the same API in the Yenlo Tenant Domain we will create.
The reason is twofold: first of course, we cannot have the same API twice in the API Manager unless we deploy it in a tenant domain. Secondly, it is good to understand how the mechanism works with a tenant domain.
Creating a tenant
Tenants in the API Manager can be added in the Configure tab. We define a simple tenant called Yenlo.
The name and other information we submit is in this case less relevant since the admin user and password are used in this case to import the API to the tenant domain.
As you can see we have a tenant defined.
For a tenant domain we use the credentials (userid / password) of the tenant, in this case the admin we just created. Since we go to a different tenant we need to include the PreserveProvider property as well
curl -H "Authorization:Basic YWRtaW5AeWVubG8uY29tOmFkbWluMQ==" -F file=@"ExportAPI.zip" -k -X POST https://localhost:9443/api-import-export-2.1.0-v2/import-api?preserveProvider=false
The preserveProvider parameter will either keep the original provider (the user who created the API) when true or use the credentials supplied by the curl command when false. For cross tenant export and import it always needs to be false.
As you can see the API is now in the yenlo.com domain. To log in you use the completely qualified admin name for that tenant, in this case email@example.com. The status however is Created, rather than published. The reason is that a published API is instantly available in the API Store and the API Gateway and therefor can be used. Edit the API using Pencil icon and change the lifecycle to Published.
Log in on the Yenlo.com store. As you can see the API is now in the tenant store as a published API. With that, the APIMGTDB is also updated so the API is fully functional (with the exception described in the caveat below) once deployed.
This blog deploys the API within a domain using the tools with default tiers. However Tiers and sequences are provider-specific. If an exported tier is not already available in the imported environment, that tier is not added to the new environment. So make sure that tiers are available before exporting the API to another environment.
So now you know how to export and import an API. In a next blog we will use swagger to import and export definitions of APIs.
If you have any questions about this blogpost contact us via the comments section of this blog. View also our WSO2 Tutorials, webinars or white papers for more technical information. Need support? We do deliver WSO2 Product Support, WSO2 Development Support, WSO2 Operational Support and WSO2 Training Programs.