Question

Example code to return api data

  • 15 October 2021
  • 3 replies
  • 63 views

Userlevel 3

Hi

I want to pull some course and session information into a web page using the API. We don’t have the expertise internally to get any help on this. Can anyone share some example javascript/json code on how to connect to the API and display some data on a web page (any data will do just as an example and I can take it from there).

Thanks


3 replies

Userlevel 5
Badge +1

Hi @Richard.Harknett 

You shouldn't use the API directly from a JavaScript embedded into a webpage. The issue is that the JavaScript must authenticate to the API, and the code of the web page is publicly accessible, so you'd be exposing the password/secret to anyone willing to look into your code. 

The call to the API should be done from the code on a server (e.g. the one that is hosting your website) and only be providing the results to be included as a part of the page. It might be in JavaScript (using node.js), but must be running on the server, so your credentials won't be exposed. Also, depending on the amount of traffic coming to your site, there may be a need to create some cashing algorithm to avoid the code causing page load delays and prevent it from exceeding Docebo’s API limits. 

I'd suggest that you work with a developer who can create server-side code to use APIs and will be able to embed the needed information into your webpage. Using APIs is not a simple copy-paste kind of activity. 

Userlevel 3

Thanks Alek

I was looking to just create a local web page to pull down some information once a week so there shouldn’t be any security concerns. But noted on the traffic and data. I’ll maybe rethink...

 

Badge

Thanks Alek

I was looking to just create a local web page to pull down some information once a week so there shouldn’t be any security concerns. But noted on the traffic and data. I’ll maybe rethink...

 

The issue with that is, the API will need to know are you accessing course data as an admin user, or as an end user who might have restrictions on what they can see. In order to know that, you must be authenticated. Just like you (probably) can’t access your LMS instance without logging in.

That authentication bit is actually quite tricky, especially if you are doing it via JWT.

I could give you some sample code in PHP from our instance, but if you aren’t able to authenticate, it’s really not something that a copy-paste will fix for you. 

Reply