|
| 1 | +--- |
| 2 | +title: "REST APIs EXAMPLES" |
| 3 | +weight: 5 |
| 4 | +description: > |
| 5 | + An advanced guide to how to use our Academy REST API to retrieve various statistics / information. |
| 6 | +categories: [Academy] |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +The following examples demonstrate how to retrieve statistics from the Academy REST API. |
| 11 | + |
| 12 | +## Get the number of active learners |
| 13 | + |
| 14 | +Use the Layer5 Cloud API to retrieve the number of *active* 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](https://docs.layer5.io/cloud/reference/api-reference/#authenticating-with-the-api)). The response JSON includes an array of user objects. |
| 15 | + |
| 16 | +## Example: cURL Request |
| 17 | + |
| 18 | +```bash |
| 19 | +curl -s -X GET "https://cloud.layer5.io/api/identity/users/online" \ |
| 20 | + -H "Authorization: Bearer <your-token>” \ |
| 21 | + | jq 'length' |
| 22 | +``` |
| 23 | +
|
| 24 | +This returns the number of active learners. |
| 25 | +``` |
| 26 | +30 |
| 27 | +``` |
| 28 | +
|
| 29 | +## Example: JavaScript (Node.js) |
| 30 | +
|
| 31 | +```javascript |
| 32 | +const token = "your-token" |
| 33 | +
|
| 34 | +fetch("https://cloud.layer5.io/api/identity/users/online", { |
| 35 | + headers: { "Authorization": "Bearer " + token } |
| 36 | +}) |
| 37 | + .then(res => res.json()) |
| 38 | + .then(users => { |
| 39 | + console.log("Active learners:", users.length); |
| 40 | + // users is an array of {id, user_id, first_name, last_name, ...} |
| 41 | + }); |
| 42 | +``` |
| 43 | +
|
| 44 | +This will print the number of active learners like this: |
| 45 | +
|
| 46 | +``` |
| 47 | +30 |
| 48 | +``` |
| 49 | +
|
| 50 | +## Example: Python Client |
| 51 | +
|
| 52 | +```python |
| 53 | +import requests |
| 54 | +
|
| 55 | +url = "https://cloud.layer5.io/api/identity/users/online" |
| 56 | +headers = {"Authorization": "Bearer <your-token>"} |
| 57 | +
|
| 58 | +resp = requests.get(url, headers=headers) |
| 59 | +online_users = resp.json() |
| 60 | +print("Active Learners:", len(online_users)) |
| 61 | +
|
| 62 | +``` |
| 63 | +
|
| 64 | +This will output: |
| 65 | +
|
| 66 | +``` |
| 67 | +Active Learners: 30 |
| 68 | +``` |
| 69 | +
|
| 70 | +
|
| 71 | +## Example: Go |
| 72 | +
|
| 73 | +```go |
| 74 | +package main |
| 75 | +
|
| 76 | +import ( |
| 77 | + "encoding/json" |
| 78 | + "fmt" |
| 79 | + "io" |
| 80 | + "net/http" |
| 81 | +) |
| 82 | +
|
| 83 | +func main() { |
| 84 | + url := "https://cloud.layer5.io/api/identity/users/online" |
| 85 | + req, _ := http.NewRequest("GET", url, nil) |
| 86 | + req.Header.Set("Authorization", "Bearer <your-token>") |
| 87 | +
|
| 88 | + client := &http.Client{} |
| 89 | + resp, _ := client.Do(req) |
| 90 | + defer resp.Body.Close() |
| 91 | +
|
| 92 | + body, _ := io.ReadAll(resp.Body) |
| 93 | + var users []map[string]interface{} |
| 94 | + json.Unmarshal(body, &users) |
| 95 | +
|
| 96 | + fmt.Println("Active users:", len(users)) |
| 97 | +} |
| 98 | +
|
| 99 | +``` |
| 100 | +
|
| 101 | +This will output something like: |
| 102 | +
|
| 103 | +``` |
| 104 | +Active users: 30 |
| 105 | +``` |
| 106 | +
|
| 107 | +
|
0 commit comments