Manage Consumer Credentials
A consumer is an application or a developer who consumes the API. The authentication plugin enabled on a route locks down access, requiring consumers to obtain credentials to access the API.
Consumers are typically created after APIs are published while developers come to apply for credentials. A consumer requires a unique username to be created. As part of the authentication configuration, you would also add one of the authentication plugins from the list above to the consumer's plugin
field.
In this tutorial, you will create a consumer with key authentication, and then use the key to access the API with key authentication.
Prerequisites
- Obtain a user account with Super Admin or API Provider role.
- Publish a service.
- Set up API authentication.
Add a Consumer
Select Services from the side navigation bar and then click the target service.
Select Consumers from the side navigation bar and then click Add Consumer.
From the Add Consumer dialog box, do the following:
- In the Gateway Group field, choose
Test Group
. - in the Name field, enter
Alice
.
- In the Gateway Group field, choose
Click Add.
Enable Key Authentication for the Consumer
Select Consumers from the side navigation bar and then select Alice.
In the Plugins field, search the
key-auth
plugin.Click the Plus icon (+) and a dialog box appears.
Apply the following configurations:
{
"key": "secret-key"
}Click Enable.
Validate
Send a Request without a Key
curl -i "http://127.0.0.1:9080/pet/1"
You should see the following output:
HTTP/1.1 401 Unauthorized
Date: Fri, 01 Sep 2023 03:06:51 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX/dev
{"message":"Missing API key found in request"}
Send a Request with a Wrong Key
curl -i "http://127.0.0.1:9080/pet/1" -H "apikey: wrongkey"
You should see the following output:
HTTP/1.1 401 Unauthorized
Date: Fri, 01 Sep 2023 03:08:00 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX/dev
{"message":"Invalid API key in request"}
Send a Request with a Correct Key
curl -i "http://127.0.0.1:9080/pet/1" -H "apikey: secret-key"
You should see the following output:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 323
Connection: keep-alive
Date: Fri, 01 Sep 2023 03:09:22 GMT
x-srv-trace: v=1;t=ada7cefb43c4848d
x-srv-span: v=1;s=4221c976c3e1b0fe
Access-Control-Allow-Origin: *
X-RateLimit-Limit: 120
X-RateLimit-Remaining: 119
X-RateLimit-Reset: 1693537822
ETag: W/"143-JIrwO+Sx1/7FTTpJ2ljwAfgaRCY"
Vary: Accept-Encoding
Server: APISIX/dev
{
"name": "Dog",
"photoUrls": [
"https://example.com/dog-1.jpg",
"https://example.com/dog-2.jpg"
],
"id": 1,
"category": {
"id": 1,
"name": "pets"
},
"tags": [
{
"id": 1,
"name": "friendly"
},
{
"id": 2,
"name": "smart"
}
],
"status": "available"
}