Create an API
What does it mean to create an API in Tyk
You have a running service with an API that you want your users to consume; you want to protect and manage access to that API using Tyk Gateway - how do you do that?For Tyk Gateway to protect and reverse proxy calls to your upstream service, you need to configure an API on Tyk Gateway. The minimum information that Tyk requires is the listen path (which is a path on the Tyk Gateway URL that you want your consumers to call) and your API URL (which is the URL of your service to which Tyk should forward requests).
This information and other configuration values are stored in an object called a Tyk API Definition. Once you have created your Tyk API Definition and deployed it in the Gateway, Tyk can start serving your consumers, forwarding their requests to your upstream service’s API. To reach a detailed guide to creating Tyk API Definitions, please choose the tab for the product you are using:
Tyk Cloud
Tyk Cloud is a fully managed service that makes it easy for API teams to create, secure, publish and maintain APIs at any scale, anywhere in the world. Tyk Cloud includes everything you need to manage your global API ecosystem: Tyk Gateways, Tyk Dashboard, Tyk Developer Portal and Universal Data Graph.To embark on your API journey with Tyk Cloud, we recommend going to our Quick Start guide. This guide will walk you through the process of creating your very first API in Tyk Cloud. For an advanced step by step guide we recommend visiting our Getting Started guide. This will explain advanced configuration steps relating to how to distribute your API across nodes, in addition to adding and testing your API.
Tyk Self-Managed
Note: Integration with your OpenAPI documentationIn Tyk v4.1 we introduced support for APIs defined according to the OpenAPI Specification v3.0.3 (OAS).
This introduces a standard way to describe the vendor-agnostic elements of an API (the OpenAPI Definition, stored as an OpenAPI Document); we take this and add Tyk-specific configuration options to create the Tyk OAS API Definition. You can import your own OpenAPI document and Tyk will use this to generate the Tyk OAS API Definition.
For a detailed tutorial on using OAS with Tyk Gateway, check out our guide to creating a Tyk OAS API Definition.
This introduces a standard way to describe the vendor-agnostic elements of an API (the OpenAPI Definition, stored as an OpenAPI Document); we take this and add Tyk-specific configuration options to create the Tyk OAS API Definition. You can import your own OpenAPI document and Tyk will use this to generate the Tyk OAS API Definition.
For a detailed tutorial on using OAS with Tyk Gateway, check out our guide to creating a Tyk OAS API Definition.
Create an API with the Dashboard
We have a video walkthrough for creating an API and testing an endpoint via Postman. We will use the Tyk Dashboard to create a very simple API that has no special elements set up.-
Select “APIs” from the “System Management” section
-
Click “ADD NEW API”
-
Set up the basic configuration of your API
- In the Overview section, add a Name for your API and select the Type of API you wish to create. We will use HTTP for this tutorial.
- In the Details section, add the Upstream URL. This is the Target URL that hosts the service to which you want to proxy incoming requests. You can configure Tyk to perform round-robin load balancing between multiple upstream servers (Target URLs) by selecting Enable round-robin load balancing; see Load Balancing for more details. For this tutorial, we will use a single upstream target: http://httpbin.org.
- Click Configure API when you have finished.
-
Set up authentication for your API
Take a look at the Authentication section:
You have the following options:
- Authentication mode: This is the method that Tyk should use to authenticate requests to call your API. Tyk supports several different authentication modes - see Client Authentication for more details on securing your API. For this tutorial, you should select
Open (Keyless)
. - Strip Authorization Data: Select this option to ensure that any security (authentication) tokens provided to authorize requests to your API on Tyk are not leaked to the upstream. You can leave this unchecked for this tutorial.
- Auth Key Header Name: The header parameter that will hold the authentication token (or key) for requests to this API; the default for this is
Authorization
. - Allow query parameter as well as header: This option allows the authentication token to be set in the query parameter, not just in the Request Header. For this tutorial, leave this unchecked.
- Use Cookie Value: Tyk also supports the use of a cookie value as an alternative authentication token location. For this tutorial, leave this unchecked.
- Enable client certificate: Tyk supports the use of Mutual TLS to authenticate requests to your API; you would use this checkbox to enable this mode. See Mutual TLS for details on implementing this feature. For this tutorial, leave this unchecked.
- Authentication mode: This is the method that Tyk should use to authenticate requests to call your API. Tyk supports several different authentication modes - see Client Authentication for more details on securing your API. For this tutorial, you should select
-
Save the API
Click SAVE
Once saved, you will be taken back to the API list, where your new API will be displayed. If you select the API from the list to open it again, the API URL will be displayed in the top of the editor. This is the URL that your consumers will need to call to invoke your API.
Create an API with the Dashboard API
It is easy to create APIs using Tyk Dashboard’s own REST API.You will need an API key for your organization (to authenticate with the Dashboard API) and issue a request using these credentials to create your new API and make it live.
-
Obtain your Tyk Dashboard API access credentials key & Dashboard URL
- From the Tyk Dashboard, select “Users” in the “System Management” section.
- Click Edit for your username, then scroll to the bottom of the page.
- Your personal API key, granting you access to the Dashboard API, is labeled Tyk Dashboard API Access Credentials key
- Store your Dashboard Key, Dashboard URL & Gateway URL as environment variables so you don’t need to keep typing them in
As you’ve got a fresh install, you will see that no APIs currently exist -
Create your first API
We’ve created a simple Tyk Classic API definition that configures the Tyk Gateway to reverse proxy to the http://httpbin.org
request/response service. The API definition object is stored here: https://bit.ly/2PdEHuv.
To load the API definition to the Gateway via the Dashboard API you issue this command:
Important Take note of the API ID returned in the
Meta
field - you will need it later as this is the Tyk Gateway’s internal identifier for the new API. -
Test your new API
You can now make a call to your new API as follows:
We sent a request to the gateway on the listen path
/httpbin
. Using this path-based-routing, the gateway was able to identify the API the client intended to target. The gateway stripped the listen path and reverse proxied the request to http://httpbin.org/get -
Protect your API
Let’s grab the API definition we created before and store the output to a file locally.
We can now edit the
api.httpbin.json
file we just created, and modify a couple of fields to enable authentication. Changeuse_keyless
fromtrue
tofalse
. Changeauth_configs.authToken.auth_header_name
toapikey
. Note Prior to ** Tyk v2.9.2**auth_configs.authToken.auth_header_name
was calledauth.auth_header_name
Then send aPUT
request back to Tyk Dashboard to update its configuration. -
Test your protected API
First try sending a request without any credentials, as before:
As you can see, you received an
HTTP 401 Unauthorized
response. Now send a request with incorrect credentials:As you can see, you received anHTTP 403 Forbidden
response. Try sending another request, this time with a valid API key. Congratulations - You have just created your first keyless API, then protected it using Tyk!
/apis
endpoint. See API definition objects for details of all the available objects. These objects encapsulate all of the settings for an API within Tyk.
Want to learn more from one of our team of engineers?
Book a demo
Tyk Open Source
Note: Integration with your OpenAPI documentationIn Tyk v4.1 we introduced support for APIs defined according to the OpenAPI Specification v3.0.3 (OAS).
This introduces a standard way to describe the vendor-agnostic elements of an API (the OpenAPI Definition, stored as an OpenAPI Document); we take this and add Tyk-specific configuration options to create the Tyk OAS API Definition. You can import your own OpenAPI document and Tyk will use this to generate the Tyk OAS API Definition.
For details on using Tyk OAS with Tyk Gateway, check out our guide to working with Tyk OAS APIs.
This introduces a standard way to describe the vendor-agnostic elements of an API (the OpenAPI Definition, stored as an OpenAPI Document); we take this and add Tyk-specific configuration options to create the Tyk OAS API Definition. You can import your own OpenAPI document and Tyk will use this to generate the Tyk OAS API Definition.
For details on using Tyk OAS with Tyk Gateway, check out our guide to working with Tyk OAS APIs.
Creating an API on Tyk Gateway
There are two ways to configure Tyk Gateway with an API definition:- Create an API with the Tyk Gateway API - Tyk Gateway has its own APIs which provides various services including the registering of Tyk API Definitions on the Gateway.
- Create an API in File-based Mode - alternatively you can create a Tyk API Definition in a file and then load it to the Gateway.
Using Tyk Gateway API
Watch our video to learn how to add an API to Tyk’s Open Source Gateway using Postman. In order to use the Gateway API to create a Tyk API Definition you will need the API key for your deployment’s Gateway API and then issue just one command to create the API and make it live.-
Make sure you know your API secret
The API key to access your Tyk Gateway API is stored in your
tyk.conf
file; the property is calledsecret
. You will need to provide this value in a header calledx-tyk-authorization
when making calls to the Gateway API. -
Create an API
To create the API, let’s send a Tyk API definition to the
/apis
endpoint on your Tyk Gateway. Remember to change thex-tyk-authorization
value (API key) in the header of your API call and set the domain name and port to target your Tyk Gateway in thecurl
command.If the command succeeds, you will see:
All APIs deployed on Tyk Gateway are given a unique
API ID
; if you don’t provide one in the Tyk API Definition when creating the API, then an API ID
will be generated automatically./apis
endpoint.Tyk API definitions encapsulate all of the settings for an API within Tyk Gateway and are discussed in detail in the API section of this documentation. Restart or hot reload Once you have created the file, you will need to either restart the Tyk Gateway, or issue a hot reload command, lets do the latter:
key_id
parameter of the response is the access key you can now use to access the API that was specified in the access_rights
section of the call.
Tyk Self Managed
Create an API Key with the Dashboard
The Tyk Dashboard is the simplest way to generate a new Key. We have a video walkthrough for creating an API Key.-
Select “Keys” from the “System Management” section
-
Click CREATE
-
Add a Policy or API to your Key
You have the option to add your new key to either an existing Policy or an existing individual API. For this Tutorial we are going to use an API.
Add an API to your Key
To select an API, you can either:
- Scroll through your API Name list
- Use the Search field
- You can also Group by Authentication Type to filter your APIs
- You can also Group by Category
-
Add Configuration Details
You use the Configuration section to set the following:
- Enable Detailed Logging. This is disabled by default and isn’t required for this tutorial
- Give your Key an Alias. This makes your key easier
- Set an Expiry time after which the key will expire. Select a value from the drop-down list. This is a required setting. See Key Expiry for more details.
- Add Tags to your policy. Any tags you add can be used when filtering Analytics Data. Tags are case sensitive.
- Add Metadata to your policy. Adding metadata such as User IDs can be used by middleware components. See Session Metadata for more details.
-
Click CREATE
A Key successfully generated pop-up will be displayed with the key shown. You must save this somewhere for future reference as it will not be displayed again. Click Copy to clipboard and paste into a text document.
That’s it, you’ve created a key - now you can try and use it.
Create an API Key with the API
To create an API key, you will need the API ID that we wish to grant the key access to. Creating the token is then an API call to the endpoint. You will also need your own API Key, to get these values:- Select Users from the System Management section.
- In the users list, click Edit for your user.
-
The API key is the Tyk Dashboard API Access Credentials, copy this somewhere you can reference it.
- Select APIs from the System Management section.
-
From the Actions menu for your API, select Copy API ID
Once you have these values, you can use them to access the Dashboard API, the below
curl
command will generate a key for one of your APIs:- Replace the
authorization
header value with your Tyk Dashboard API Access Credentials - Replace the API ID (
ad5004d961a147d4649fd3216694ebe2
) with your API ID - It’s recommended to validate the JSON using JSON validator to avoid any
malformed input
error
- Replace the
Importing a definition as a version in an existing API:
Add a version to a definition:When creating a new definition from an OAS 3.0 spec, you will have to manually add the listen path after the API is created.
Importing a Swagger document as a version into an existing API
Add a version to a definition:Mocks
Tyk supports API mocking using our versioninguse_extended_paths
setup, adding mocked URL data to one of the three list types (white_list, black_list or ignored). In order to handle a mocked path, use an entry that has action
set to reply
:
Import APIs via the Dashboard API
Import API - Swagger
Property | Description |
---|---|
Resource URL | /api/import/swagger/ |
Method | POST |
Type | None |
Body | None |
Param | None |
Sample Request
insert_into_api
: If set totrue
the import will replace an existing API. Setting tofalse
will import into a new API.api_id
: The internal MongoDB object id for your API.version_name
: Your versioning convention name for the imported API.upstream_url
: The URL the API is served by.
Sample Response
Import API - Blueprint
Property | Description |
---|---|
Resource URL | /api/import/blueprint/ |
Method | POST |
Type | None |
Body | None |
Param | None |
Sample Request
insert_into_api
: If set totrue
the import will replace an existing API. Setting tofalse
will import into a new API.api_id
: The internal MongoDB object id for your API.as_mock
: If set to true, enables our mocking support for Blueprint imported API. See Mocks above for more details.version_name
: Your versioning convention name for the imported API.upstream_url
: The URL the API is served by.
Sample Response
Import APIs via the Dashboard UI
-
Select “APIs” from the “System Management” section
-
Click “IMPORT API”
Tyk supports the following import options:
- From an Existing Tyk API definition
- From a Apiary Blueprint (JSON) file
- From a Swagger/OpenAPI (JSON only) file
- From a SOAP WSDL definition file (new from v1.9)
For WSDL:
-
Enter API Information
You need to enter the following information:
- Your Upstream Target
- A Version Name (optional)
- An optional Service Name and Port (WSDL only)
- Copy code into the editor
- Click “Generate API” Your API will appear in your APIs list. If you select EDIT from the ACTIONS drop-down list, you can see the endpoints (from the Endpoint Designer) that have been created as part of the import process.
Creating a new API Version by importing an API Definition using Tyk Dashboard
As well as importing new APIs, with Tyk, you can also use import to create a new version of an existing Tyk Classic API.-
Open the API Designer page and select Import Version from the Options drop-down.
- Select either OpenAPI (v2.0 or 3.0) or WSDL/XML as your source API
-
You need to add a new API Version Name. Upstream URL is optional.
-
Click Import API.
- Select the Versions tab and your new version will be available.
- Open the Endpoint Designer for your API and select your new version from Edit Version.
- You will see all the endpoints are saved for your new version.

Import from an OpenAPI v2.0 Document
-
From the Import API screen, select OpenAPI.
-
Paste your OAS v2.0 compliant definition into the code editor.
-
Note that the Dashboard has detected that an OAS v2.0 definition has been imported and you need to specify an upstream URL field to proceed.
-
Click Import API.
Your API will be added to your list of APIs.