Skip to content

Commit fda7444

Browse files
authored
Merge pull request #801 from Siddhi-sahu/feat/add-rest-apis-examples
[Docs] Academy: Example use of REST APIs
2 parents dd117a5 + 36d17d0 commit fda7444

File tree

1 file changed

+105
-1
lines changed

1 file changed

+105
-1
lines changed

content/en/cloud/reference/api-reference.md

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,109 @@ curl <protocol>://<Layer5-cloud-hostname>/<API> \
2828
## All API Endpoints
2929

3030
{{< alert type="info" >}}
31-
<a href="https://cloud.layer5.io/system/api/docs">Open API Endpoints in new window <i class="fa fa-external-link" aria-hidden="true"></i></a>
31+
<a href="https://cloud.layer5.io/system/api/docs" target="_blank">Open API Endpoints in new window <i class="fa fa-external-link" aria-hidden="true"></i></a>
3232
{{< /alert >}}
33+
34+
## API Example
35+
36+
The following example demonstrate how to retrieve information from the Academy REST APIs.
37+
38+
### Get the total number of registered learners in Academy
39+
40+
Use the Layer5 Cloud API to retrieve the *total* number of registered learners. Pass your [Security Token](https://docs.layer5.io/cloud/security/tokens/) as a Bearer token in the `Authorization` header (as shown in [Authenticating with API](/cloud/reference/api-reference/#authenticating-with-the-api)). The response JSON includes an array of user objects.
41+
42+
43+
{{< tabpane >}}
44+
{{< tab header="cURL" >}}
45+
curl -s -X GET "https://cloud.layer5.io/api/academy/cirricula" \
46+
-H "Authorization: Bearer <Your-Token>" \
47+
| jq '[.data[].registration_count] | add'
48+
49+
{{< /tab >}}
50+
51+
{{< tab header="JavaScript" >}}
52+
53+
const token = "Your-Token"
54+
55+
async function getTotalLearners() {
56+
const res = await fetch("https://cloud.layer5.io/api/academy/cirricula", {
57+
headers: { Authorization: `Bearer ${token}` },
58+
});
59+
const data = await res.json();
60+
const total = data.data.reduce((sum, path) => sum + path.registration_count, 0);
61+
console.log(total);
62+
}
63+
64+
getTotalLearners();
65+
66+
{{< /tab >}}
67+
68+
{{< tab header="Python" >}}
69+
70+
import requests
71+
72+
url = "https://cloud.layer5.io/api/academy/cirricula"
73+
headers = {"Authorization": "Bearer <Your-Token>"}
74+
75+
res = requests.get(url, headers=headers)
76+
data = res.json()
77+
total = sum(item["registration_count"] for item in data["data"])
78+
print(total)
79+
80+
{{< /tab >}}
81+
82+
{{< tab header="Golang" >}}
83+
84+
package main
85+
86+
import (
87+
"encoding/json"
88+
"fmt"
89+
"io"
90+
"net/http"
91+
)
92+
93+
type Path struct {
94+
RegistrationCount int `json:"registration_count"`
95+
}
96+
97+
type Response struct {
98+
Data []Path `json:"data"`
99+
}
100+
101+
func main() {
102+
url := "https://cloud.layer5.io/api/academy/cirricula"
103+
104+
req, _ := http.NewRequest("GET", url, nil)
105+
req.Header.Set("Authorization", "Bearer <your-token>")
106+
107+
client := &http.Client{}
108+
res, err := client.Do(req)
109+
if err != nil {
110+
panic(err)
111+
}
112+
defer res.Body.Close()
113+
114+
body, _ := io.ReadAll(res.Body)
115+
116+
var response Response
117+
if err := json.Unmarshal(body, &response); err != nil {
118+
panic(err)
119+
}
120+
121+
total := 0
122+
for _, path := range response.Data {
123+
total += path.RegistrationCount
124+
}
125+
126+
fmt.Println(total)
127+
}
128+
129+
{{< /tab >}}
130+
131+
{{< /tabpane >}}
132+
133+
This returns the number of Total registered learners:
134+
```
135+
130
136+
```

0 commit comments

Comments
 (0)