1. Home
  2. Tyk Open Source API Gateway v2.x
  3. Tutorials
  4. Set up your first API (Community Edition)

Set up your first API (Community Edition)

Tyk can be used with the dashboard or Mongo DB, this means feeding API Definitions to it using JSON documents stored on the disk.

With Tyk, this can all be achieved using the REST API, so we’ll be setting up a simple proxy to HttpBin, then generate a key and then test the API, alternatively, you can just put the JSON documents into your Tyk installations /apps/ folder directly without using the API.

Step 1. Create an API

This tutorial presupposes that the Tyk Gateway is running and is in file-based mode, and that you have the secret that you set in your tyk.conf on hand (if you don’t, then open your tyk.conf file, it will be under /opt/tyk-gateway/tyk.conf and copy the field marked secret)

To create the API, lets send a definition to the gateway:

```

curl -H "x-tyk-authorization: 352d20ee67be67f6340b4c0605b044b7"
 -s
 -H "Content-Type: application/json"
 -X POST
 -d '{
    "name": "Test API",
    "slug": "test-api",
    "api_id": "1",
    "org_id": "53ac07777cbb8c2d53000002",
    "auth": {
        "auth_header_name": "Authorization"
    },
    "definition": {
        "location": "header",
        "key": "x-api-version"
    },
    "version_data": {
        "not_versioned": true,
        "versions": {
            "Default": {
                "name": "Default",
                "use_extended_paths": true
            }
        }
    },
    "proxy": {
        "listen_path": "/test-api/",
        "target_url": "http://httpbin.org/",
        "strip_listen_path": true
    },
    "active": true,
    "domain": ""
 }'
 http://{YOUR-GATEWAY-HOSTNAME}:8080/tyk/apis/ | python -mjson.tool

```

If the command succeeds, you will see:

{
    "action": "added",
    "key": "1",
    "status": "ok"
}

Step 2. Load the API into the Gateway

In the first step all we’ve done is created the API and flushed it to disk in the gateway (if you check the apps directory, you should see a new API file).

To load the API, we need to run a hot reload on the gateway – this is very straightforward:

curl -H "x-tyk-authorization: 352d20ee67be67f6340b4c0605b044b7" 
-s 
-H "Content-Type: application/json" 
http://{YOUR-GATEWAY-HOSTNAME}:8080/tyk/reload/ | python -mjson.tool

You should see the following output:

{
    "error": "",
    "status": "ok"
}

If you are monitoring your API Gateway output, you might see something like this:

INFO[0912] Reload URL Structure - Scheduled
INFO[0922] Loading API Specification from test_apps/1.json
INFO[0922] Loading API Specification from test_apps/test_api.json
INFO[0922] Detected 1 APIs
INFO[0922] --> Loading API: Test API
INFO[0922] ----> Tracking: (no host)
INFO[0922] ----> Checking security policy: Token
INFO[0922] Loading uptime tests...
INFO[0922] API reload complete

Step 4 – Test it:

To access the gateway we can do the same thing we did in the first tutorial:

curl -H "Authorization: null" http://127.0.0.1:8080/test-api/get

Output:
-------
{
    "error": "Key not authorised"
}

Step 5 – Create a key:

Now since we are using file-based mode, we will need to use the tyk Gateway REST API to generate a key to access this new API we’ve created.

Remember how when we created the test API, we called it id: 1? We’ll use that as our access permission:

```
curl -H "x-tyk-authorization: 352d20ee67be67f6340b4c0605b044b7"
 -s
 -H "Content-Type: application/json"
 -X POST
 -d '{
    "allowance": 1000,
    "rate": 1000,
    "per": 1,
    "expires": -1,
    "quota_max": -1,
    "quota_renews": 1449051461,
    "quota_remaining": -1,
    "quota_renewal_rate": 60,
    "access_rights": {
        "1": {
            "api_id": "1",
            "api_name": "Test API",
            "versions": ["Default"]
        }
    },
    "meta_data": {}
 }'
 http://{YOUR-GATEWAY-HOSTNAME}:8080/tyk/keys/create | python -mjson.tool
```

You will see a response with your new Key ID:

{
    "action": "create",
    "key": "c2cb92a78f944e9a46de793fe28e847e",
    "status": "ok"
}

Step 6 – Test it:

Lets re-use that command we used in Step 4:

curl -H "Authorization: c2cb92a78f944e9a46de793fe28e847e" http://127.0.0.1:8080/test-api/get

Output:
-------
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip",
    "Authorization": "c2cb92a78f944e9a46de793fe28e847e",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.35.0"
  },
  "origin": "178.62.11.62, 178.62.11.62",
  "url": "http://httpbin.org/get"
}

And there you have it, adding an API to Tyk using it’s own REST API and then adding a token to access that API.

The API we created here is very simple and has no real settings enabled, we recommend taking a look at the extended functionality of Tyk API Definition objects as well as a breakdown of all the various features throughout the documentation.