Skip to content

Commit 04a7495

Browse files
Finish the rough draft
Signed-off-by: Diana <75819066+cloudjumpercat@users.noreply.github.com>
1 parent b1bd0a5 commit 04a7495

File tree

1 file changed

+169
-29
lines changed

1 file changed

+169
-29
lines changed

app/_how-tos/get-started-with-metering-and-billing.md

Lines changed: 169 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -56,42 +56,182 @@ next_steps:
5656
automated_tests: false
5757
---
5858

59+
## Create a Consumer
60+
61+
Create the two consumers, one for the free and one for premium. These will be mapped to our customers/subjects later on
62+
63+
[Consumers](/gateway/entities/consumer/) let you identify the client that's interacting with {{site.base_gateway}}.
64+
We're going to use key [authentication](/gateway/authentication/) in this tutorial, so the Consumer needs an API key to access any {{site.base_gateway}} Services.
65+
66+
{% entity_examples %}
67+
entities:
68+
consumers:
69+
- username: ariel
70+
keyauth_credentials:
71+
- key: ariel-key
72+
- username: rosario
73+
keyauth_credentials:
74+
- key: rosario-key
75+
{% endentity_examples %}
76+
77+
## Enable authentication
78+
79+
Authentication lets you identify a Consumer so that you can apply rate limiting.
80+
This example uses the [Key Authentication](/plugins/key-auth/) plugin, but you can use any [authentication plugin](/plugins/?category=authentication) that you prefer.
81+
82+
Enable the plugin globally, which means it applies to all {{site.base_gateway}} Services and Routes:
83+
84+
{% entity_examples %}
85+
entities:
86+
plugins:
87+
- name: key-auth
88+
config:
89+
key_names:
90+
- apikey
91+
{% endentity_examples %}
92+
93+
## Enable rate limiting
94+
95+
Set rate limits for the consumers. Free consumer gets 1,000 requests for example_service and premium gets 5,000 requests for the same service/api.
96+
97+
Enable the [Rate Limiting plugin](/plugins/rate-limiting/) for the Consumer.
98+
In this example, the limit is 5 requests per minute and 1000 requests per hour.
99+
100+
{% entity_examples %}
101+
entities:
102+
plugins:
103+
- name: rate-limiting
104+
consumer: ariel
105+
config:
106+
minute: 5
107+
hour: 1000
108+
- name: rate-limiting
109+
consumer: rosario
110+
config:
111+
minute: 5
112+
hour: 1000
113+
{% endentity_examples %}
114+
115+
## Configure ACL plugin
116+
117+
Configure the ACL plugin to only allow access to the service for customers who have signed up, so our two consumers:
118+
119+
{% entity_examples %}
120+
entities:
121+
plugins:
122+
- name: acl
123+
service: example_service
124+
config:
125+
allow:
126+
- free
127+
- premium
128+
{% endentity_examples %}
129+
59130
## Create a system access token in Konnect
60131
to ingest usage events?
61132

62133
## Create a meter
63134

64-
## Send your first usage
65-
66-
Ingest usage events in CloudEvents format:
67-
68-
```
69-
curl -X POST https://openmeter.cloud/api/v1/events \
70-
-H 'Content-Type: application/cloudevents+json' \
71-
-H 'Authorization: Bearer <API_KEY>' \
72-
--data-raw '
73-
{
74-
"specversion": "1.0",
75-
"type": "prompt",
76-
"id": "00001",
77-
"time": "2024-01-01T00:00:00.001Z",
78-
"source": "chat-app",
79-
"subject": "customer-1",
80-
"data": {
81-
"tokens": "123456",
82-
"model": "gpt4-turbo",
83-
"type": "output"
84-
}
85-
}
86-
'
87-
```
88-
89-
## Query your meter
90-
91-
Query the meter to see the usage.
135+
1. In the {{site.konnect_short_name}} sidebar, click **Metering & Billing**.
136+
1. Enable **API Gateway Requests**.
137+
138+
This will pull in request proxied by your API Gateway in {{site.konnect_short_name}} to Metering & Billing.
92139

93140
## Create a feature
94141

142+
Creating a feature that is linked to our service:
143+
144+
1. In the {{site.konnect_short_name}} sidebar, click **Metering & Billing**.
145+
1. In the Metering & Billing sidebar, click **Product Catalog**.
146+
1. Click **Create Feature**>
147+
1. In the **Name** field, enter `example_service`.
148+
1. From the **Meter** dropdown menu, select "kong_konnect_api_request".
149+
1. Click **Add group by filter**.
150+
1. From the **Group by** dropdown menu, select "service_name".
151+
1. From the **Operator** dropdown menu, select "Equals".
152+
1. From the **Value** dropdown menu, select "example_service".
153+
1. Click **Save**.
154+
155+
95156
## Create a plan
96157

97-
## Set up billing
158+
Creating plans for our feature.
159+
160+
1. In the {{site.konnect_short_name}} sidebar, click **Metering & Billing**.
161+
1. In the Metering & Billing sidebar, click **Product Catalog**.
162+
1. Click the **Plans** tab.
163+
1. Click **Create Plan**.
164+
1. In the **Name** field, enter `Free`.
165+
1. In the **Billing cadence** dropdown menu, select "1 month".
166+
1. Click **Save**.
167+
1. Click **Add Rate Card**.
168+
1. From the **Feature** dropdown menu, select "example_service".
169+
1. Click **Next**.
170+
1. From the **Pricing model** dropdown menu, select "Free".
171+
1. Click **Next**.
172+
1. From the **Entitlements** dropdown, select "Metered".
173+
1. From the **Usage Period Interval** dropdown, select "Monthly".
174+
1. In the **Allowance for Period** field, enter `1000`.
175+
1. Click **Save Rate Card**.
176+
1. Click **Publish Plan**.
177+
1. Navigate back to **Plans** in the breadcrumbs.
178+
1. Click **Create Plan**.
179+
1. In the **Name** field, enter `Premium`.
180+
1. In the **Billing cadence** dropdown menu, select "1 month".
181+
1. Click **Save**.
182+
1. From the **Feature** dropdown menu, select "example_service".
183+
1. Click **Next**.
184+
1. From the **Pricing model** dropdown menu, select "Package".
185+
1. In the **Price per package** field, enter `1`.
186+
1. In the **Quantity per package** field, enter `5000`.
187+
1. Click **Next**.
188+
1. From the **Entitlements** dropdown, select "Metered".
189+
1. In the **Allowance for Period** field, enter `5000`.
190+
1. Click **Save Rate Card**.
191+
1. Click **Publish Plan**.
192+
193+
## Map Consumers to customers
194+
195+
1. In the {{site.konnect_short_name}} sidebar, click **Metering & Billing**.
196+
1. In the Metering & Billing sidebar, click **Billing**.
197+
1. Click **Create Customer**.
198+
1. In the **Name** field, enter `Ariel`.
199+
1. In the **Include usage from** dropdown, select "Ariel".
200+
1. Click **Save**.
201+
1. Click the **Subscriptions** tab.
202+
1. Click **Create a Subscription**.
203+
1. From the **Subscribed Plan** dropdown, select "Free".
204+
1. Click **Next**.
205+
1. Click **Create Subscription**.
206+
1. Navigate back to **Customers** in the breadcrumbs.
207+
1. Click **Create Customer**.
208+
1. In the **Name** field, enter `Rosario`.
209+
1. In the **Include usage from** dropdown, select "Rosario".
210+
1. Click **Save**.
211+
1. Click the **Subscriptions** tab.
212+
1. Click **Create a Subscription**.
213+
1. From the **Subscribed Plan** dropdown, select "Premium".
214+
1. Click **Next**.
215+
1. Click **Create Subscription**.
216+
217+
Note: Want to delete a customer? Cancel their subscription first and then you can delete them.
218+
219+
## Validate??
220+
221+
Customer uses the service and we see the invoice triggered?
222+
223+
You can run the following command to test the rate limiting as the Consumer:
224+
225+
{% validation rate-limit-check %}
226+
iterations: 6
227+
url: '/anything'
228+
headers:
229+
- 'apikey:ariel-key'
230+
{% endvalidation %}
231+
232+
Now, check the invoice that was created in Metering & Billing:
233+
234+
1. In the {{site.konnect_short_name}} sidebar, click **Metering & Billing**.
235+
1. In the Metering & Billing sidebar, click **Billing**.
236+
1. Click the **Invoices** tab.
237+
1. See the invoice created?

0 commit comments

Comments
 (0)