Learning to use APIs
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Learning to use APIs
(I really hope I am not asking too many questions -- tell me if I am overasking! Don't know the netiquette)
I am looking to write an app that pulls in current stock prices using an API like Morningstar's API. But, I have no idea how to use an API with LiveCode. Does anyone know where I can learn?
Thanks! Eric
I am looking to write an app that pulls in current stock prices using an API like Morningstar's API. But, I have no idea how to use an API with LiveCode. Does anyone know where I can learn?
Thanks! Eric
Re: Learning to use APIs
Cyril had a pretty good lesson on it here. In fact, his whole site is full of lessons, tidbits, code, and all kinds of other useful things, I'd HIGHLY recommend starting on the home page after you find out what you want to know.

Re: Learning to use APIs
Exactly the opposite. You are not asking enough questions. Several people on this forum live for this.
Craig
Craig
-
- VIP Livecode Opensource Backer
- Posts: 10048
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Learning to use APIs
Like Craig says, you cannot ask too many questions. Feel free to fire away any that come to mind.
Most APIs use HTTP. How well do you understand HTTP? Once you grasp the basics of that protocol, using it to obtain data from a server gets much easier.
More challenging than using the API itself can be authenticating, usually a first step before the request for data can be sent to a server. The two most common authentication methods are HTTP Basic Authentication (which embeds the user name and password in the request URL), and OAuth2.
OAuth2 is well supported by a library in LiveCode on all platforms but Linux. It's easy to use, but it's helpful to understand how AOuth2 works too use it well. There are many resources on the web for learning how OAuth2 works.
Which authentication method is prescribed in the Morningstar API docs?
Most APIs use HTTP. How well do you understand HTTP? Once you grasp the basics of that protocol, using it to obtain data from a server gets much easier.
More challenging than using the API itself can be authenticating, usually a first step before the request for data can be sent to a server. The two most common authentication methods are HTTP Basic Authentication (which embeds the user name and password in the request URL), and OAuth2.
OAuth2 is well supported by a library in LiveCode on all platforms but Linux. It's easy to use, but it's helpful to understand how AOuth2 works too use it well. There are many resources on the web for learning how OAuth2 works.
Which authentication method is prescribed in the Morningstar API docs?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Learning to use APIs
I really, really appreciate that. I feel like I shouldn't be wasting people's time, but I am so at the beginning of my programming journey. This forum has really helped me.
I went to a place called "Rapid API" where I signed up for the Morning Star API. They offer snippets of code in like 20 languages, but not LiveCode (of course
) So, I have copied JAVA (unirest) of which I know absolutely none. Can anyone figure out from this how to translate to LiveCode?
I went to a place called "Rapid API" where I signed up for the Morning Star API. They offer snippets of code in like 20 languages, but not LiveCode (of course

Here it is in Python:HttpResponse<String> response = Unirest.get("https://morning-star.p.rapidapi.com/sto ... 0P0000OQN8")
.header("x-rapidapi-host", "morning-star.p.rapidapi.com")
.header("x-rapidapi-key", "XXXXX")
.asString();
But to answer your question, I do not know HTTP or oAuth2. (I don't even really know what those refer to...) In this case, I am hoping to learn how I can access data, without necessarily learning all the mechanics. If possible...headers = {
'x-rapidapi-host': "morning-star.p.rapidapi.com",
'x-rapidapi-key': "XXXXX"
}
conn.request("GET", "/stock/v2/get-mini-chart-realtime-data?performanceId=0P0000OQN8", headers=headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
Last edited by egolombek on Wed Jun 10, 2020 9:29 pm, edited 1 time in total.
-
- VIP Livecode Opensource Backer
- Posts: 10048
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Learning to use APIs
Possible, but not optimal. And likely not desirable, given the potential risks with network applications.
You're entering programming at a uniquely challenging moment in history. Everything is connected by an open Internet, which enables app designers to deliver empowering solutions, but it also enables the bad guys to do great damage.
In the olden days users were content to read and write files only on their own machine. Programming was simple then. Without connectivity, the only way someone could muck with your data was if they burgled your home and physically stole the computer. If that happened you may have bigger concerns than just the data an app uses.
These days data and the apps that use it are often in different locations, with potentially several billion other people between you and your data. And some of those are aren't nice. And a few of those are also smart.
The good news is that we're not alone. Every industry uses the internet, so there are few wheels we need to reinvent. But we do need to learn them, at least at ab basic level, so we can use them well.
HTTP is a beautiful protocol. You can make your own protocol, but most of the time what you need is just a way to send a request and get a reply, and HTTP does that wonderfully. So well, in fact, you'll probably never need to write a custom protocol through your entire career. Most of what you'll do will be done through HTTP.
HTTP is simple, yet extensible. So learning only a few things let's you use it in infinite ways.
Skim the Wikipedia page on it, and come back with questions. Some day I'll get around to writing a book on networking in LiveCode, but for now there's enough out there to do well.
When learning something new, details can be overwhelming. Don't worry. With HTTP a very small number of principles make everything else fall into place.
Once you have a basic understanding of using HTTP, some services like APIs will require a means of authenticating the request, making sure the requester is who they say they are.
There are a great many authentication methods, and some people even roll their own (not recommended nor usually even necessary). You can ignore most of them.
Most services you'll access will only use either Basic Auth or OAuth2. Basic is super-simple, and while OAuth2 is complex under the hood you don't need to worry about most of those details when using LiveCode because the included OAuth2 library takes care of the heavy lifting.
The most fun way to learn things is with a concrete example for something you want to accomplish. And you have one, so we don't need to explore everything all at once, just the parts you need to get this task done. Anything else can come later as your needs grow.
So let's start at the beginning, with the most important thing to learn: dealing with vendor documentation.

The code examples you provided will be helpful later. For now we need to know how to gain access to the system at all. And that will depend on which authentication method they use.
So your first task is to review their docs to find out what's needed to authenticate.
Good docs will make that easy to find, usually in the first section or two because everything else will depend on it.
So dive in and let us know what you find. If their writing isn't clear (many great engineers are poor writers, so that happens often) let us know how far you got and include the URL to the page where you found what you're asking about, and we'll pick it up from there.
The art of programming is really the art of learning. Those who do well are those who enjoy learning new things, a necessity in a field defined by perpetual change.
But you don't need to learn everything at once (and that's not even how learning works anyway). We'll help guide you step by step, as others helped us before, and over time you'll find yourself helping others as well.
Looking forward to what you learn about their auth methods...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Learning to use APIs
I would like to pre-order that. And apologise for distracting from the pertinent and informative tracts above.FourthWorld wrote: ↑Wed Jun 10, 2020 5:48 pmSome day I'll get around to writing a book on networking in LiveCode
-
- VIP Livecode Opensource Backer
- Posts: 10048
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: Learning to use APIs
No worries. Thanks for the encouragement on the book. Very motivating.SparkOut wrote: ↑Wed Jun 10, 2020 7:11 pmI would like to pre-order that. And apologise for distracting from the pertinent and informative tracts above.FourthWorld wrote: ↑Wed Jun 10, 2020 5:48 pmSome day I'll get around to writing a book on networking in LiveCode
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Learning to use APIs
I appreciate your thoughtful response with encouragement to learn http. I took your advice and learned a bit about http (and then tcp/ip) -- videos were more helpful than Wikipedia, but it led me to realize I need to build a custom http tsNetCustomSync request. To my shock and surprise, it actually worked!
Here's what I did. I have XXXXXed the key, for privacy:

Here's what I did. I have XXXXXed the key, for privacy:
Still much to learn, but nice to get it to connect!on mouseUp
put "https://morning-star.p.rapidapi.com/sto ... 0P0000OQN8" into pURL
put "x-rapidapi-host: morning-star.p.rapidapi.com"&return&"x-rapidapi-key:XXXXX"&return&"useQueryString: true" into tHeaders
put tsNetCustomSync(pURL, "GET", tHeaders, rOutHeaders, rResult, rBytes) into cd fld tOut
answer rResult
end mouseUp
