Working with API Templates using the Dashboard API
Last updated: 9 minutes read.
API Templates are an API governance feature provided to streamline the process of creating Tyk OAS APIs. An API template is an asset managed by Tyk Dashboard that is used as the starting point - a blueprint - from which you can create a new Tyk OAS API definition.
The Tyk Dashboard API provides the following functionality to support working with API templates:
- registering a template with Tyk Dashboard
- applying a template when creating an API from an OpenAPI document
- applying a template when creating an API from a Tyk OAS API definition
Note
API Templates are exclusive to Tyk OAS APIs.
Structure of an API template
An API template asset has the following structure:
id
: a unique string type identifier for the templatekind
: the asset type, which is set tooas-template
name
: human-readable name for the templatedescription
: a short description of the template, that could be used for example to indicate the configuration held within the templatedata
: a Tyk OAS API definition, the content of which will be used for templating APIs_id
: a unique identifier assigned by Tyk when the template is registered in the Dashboard database
Registering a template with Tyk Dashboard
To register an API template with Tyk, you pass the asset in the body of a POST
request to the dashboard’s /api/assets
endpoint.
For example, if you send this command to the endpoint:
|
|
Tyk will respond with HTTP 201 Created
and will provide this payload in response:
{
"Status": "success",
"Message": "asset created",
"Meta": "65e8c352cb71918520ff660c",
"ID": "my-unique-template-id"
}
Here Meta
contains the database ID (where the asset has been registered in the persistent storage) and ID
contains the unique identifier for the template. This unique identifier will be automatically generated by Tyk if none was provided in the id
of the template asset provided in the curl
request.
Applying a template when creating an API from an OpenAPI document
When creating an API on Tyk using an OpenAPI document describing your upstream service, you can use the /apis/oas/import
endpoint to import the OpenAPI description and apply it to your API.
If you have a template registered with your Dashboard, you can use this as the starting point for your new API. Tyk will combine the OpenAPI document with the template, automating the configuration of any element in the Tyk OAS API definition as defined in your chosen template.
You’ll need to identify the template to be used during the import. You can use either its unique id
or the database ID that was assigned when the template was registered with Tyk Dashboard. You provide either the id
or _id
in the templateID
query parameter in the call to /oapis/oas/import
.
For example:
|
|
Tyk will respond with HTTP 200 OK
and will provide this payload in response:
{
"Status": "OK",
"Message": "API created",
"Meta": "65e8c4f4cb71918520ff660d",
"ID": "970560005b564c4755f1db51ca5660e6"
}
Here Meta
contains the database ID (where the API has been registered in the persistent storage) and ID
contains the unique identifier for the API. This unique identifier will be automatically generated by Tyk as none was provided in the id
field of the x-tyk-api-gateway.info
field provided in the curl
request.
The new Tyk OAS API will have this definition, combining the OpenAPI description provided in the body of the curl
request with the template with Id my-unique-template-id
:
|
|
Note that the GET /xml
endpoint from the OpenAPI description and the POST /anything
endpoint from the template (complete with requestSizeLimit
middleware) have both been defined in the API definition. API-level caching has been enabled, as configured in the template. Tyk has included the server
entry from the OpenAPI description (which points to the upstream server) and added the API URL on Tyk Gateway (as explained here).
Applying a template when creating an API from a Tyk OAS API definition
When creating an API using a complete Tyk OAS API definition (which includes x-tyk-api-gateway
), you can use the /apis/oas
endpoint to import the API defintiion.
If you have a template registered with your Dashboard, you can use this as the starting point for your new API. Tyk will combine the API definition with the template, automating the configuration of any element defined in your chosen template.
You’ll need to identify the template to be used during the import. You can use either its unique id
or the database ID that was assigned when the template was registered with Tyk Dashboard. You provide either the id
or _id
in the templateID
query parameter in the call to /apis/oas
.
For example:
|
|
Tyk will respond with HTTP 200 OK
and will provide this payload in response:
{
"Status": "OK",
"Message": "API created",
"Meta": "65e98ca5cb71918520ff6616",
"ID": "b8b693c5e28a49154659232ca615a7e8"
}
Here Meta
contains the database ID (where the API has been registered in the persistent storage) and ID
contains the unique identifier for the API. This unique identifier will be automatically generated by Tyk as none was provided in the id
field of the x-tyk-api-gateway.info
field provided in the curl
request.
The new Tyk OAS API will have this definition, combining the Tyk OAS API definition provided in the body of the curl
request with the template with Id my-unique-template-id
:
|
|
Note that the GET /json
endpoint from the OpenAPI description and the POST /anything
endpoint from the template (complete with requestSizeLimit
middleware) have both been defined in the API definition. API-level caching has been enabled, as configured in the template.