Hi, welcome to the possibly most exciting video of this section here, you'll learn how to build your
own API.
This is going to be an API which serves the current currency rates to the client who asks for the rates.
So this would be a recipe that means the client, which could be another application.
They will make requests to our API, to your API through URLs.
So I already built mine.
This is my API so you can make a request to this web site.
For example, if you want to get the current rates between USD and euros, you write out the answer
as the Dash euro.
And of course, this first part now, probably this link will not work now.
And depending on whether I'm stopping this, this service or not, in the future, both you learn how
to build the exact API in this video.
So let's try another currency rates, for example, the USD to JPY.
This is the codes for the Japanese yen, and you get the new rate, which is between USD and JPY.
So from that to that's currency, that's the rate for one units of USD.
So let's jump into our python environment and build this exact API, and you'll also learn how to publish
this to make this public right away.
The condition is that if you want to make this public right away, though, the URL links off your API.
You will need to use Ripple.
If you use your own local ID, then you have to manually deploy your web API to a service such as Python
Anywhere or Heroku to make the URLs public.
Otherwise, it would be an API in your local hosting your computer with repo.
This is very easy to do, so you don't have to use Python anywhere or Heroku with Ripple.
It will be public right away.
So let's start.
How do we build an API?
Well, an API is a web app.
So you need to use one of the Python web frameworks for that.
It's called the Flask or Django.
Now will use flask because Flask is easier to set up.
An API is a small web app, so Flask tends to be better when it comes to creating smaller web apps.
So we're going to use Flask and therefore let's import from flask import the flask class.
Once you do that, you create an app instance using that flask glass.
Let's give it a name to the instance, and it's good practice to use underscored on the name, on the
server, on the score.
So this is a special variable which holds the string or the script main.
So it's an automatic way to to name it.
And you need to give that as an argument to flask.
Then our API with consist basically of two pages to end points, one will be the home page where we
will write basically the documentation of our API.
Therefore, the first end point will be a home page or a slash means the home page of the app.
If you want to go to another page, you could write something like home or something like that.
But that's OK.
And that should be followed by a function.
And you should return something there.
So it could be a string, but you could also return to a such as a J1
currency rates API and then write some paragraph.
Maybe after that.
So these are the tags that to formats, the texts on the home page.
I'm just going to write an example.
You were out here for users to know how they can access the you or else of the API so numb in the other
function.
We're going to configure the URLs in the form of something like API V1, which means this will be version
one of the API and that I could try something like USD to Euro, for example.
So let's leave it like that and see what we get on the home page.
Say up to run.
And if you are using repo, you need to parse this argument to see the web page life public on the web.
So hosts with that.
Otherwise, if you're locally on your idea, you don't need to parse that.
So, yeah, I would run this.
Flask will be installed if you are locally on your I.D., then you need to install it manually with
PIP Install Flask.
So PIP Install Flask with a lowercase F.
So that's our website.
It's running and it's public, so it shows up in here in this window, but you can also browse it's
on your main browser.
So that's the domain of my web app.
Create your own currency rates API.
This is automatically given by Ripple, followed by my oracle username and Ripple.
That's cool.
Now, if you're locally, this could be something like local host Colin 5000.
So it will also show in here of the URL.
You can click that and it'll take you to the same page, but on Rebel.
It takes you directly to the public bureau.
Now this is the heading that we added in here currency rates API.
And that's the paragraph.
So we're telling the user now that if they enter this after
the domain, so if they enter that appended to here, then they should access the rates for that USD
two euro currency exchange.
This currently seems not fine because we haven't configured this URL yet.
So let's go back to Python and configured that route.
That's ends points.
So we're talking about, uh, again, it's um, let's go back.
This one in here.
So I'm going to copy that and paste it in here.
Yeah, it's also here.
So slash API slash V1 and then this is static for now.
So to make these dynamic, we want to say we want to use a specific syntax with these brackets.
Then here you see in on the score card or any variable.
So that's the inputs currency and then the outputs currency, let's say, out on the score card.
Now what are these?
So when the user enters here at the end, they enter a USD Dash JPY, for example, USD will go in here
and then the dashti says it is, and then JPY will go in here.
So incur is a variable which would be equal to USD for that particular user, and our ticker would be
equal to JPY also for that user.
Now these variables now will go as arguments of this function incurred out cur.
So incur will go there.
Altucher will go there.
Then we have access to these values inside this function.
So what do we do next?
Well, next we need the code which scrapes the currency reads from somewhere on the web so that we have
access to that rate here.
And then we return vertebrates to the API so that it shows here in JASON data.
So how do we give the rates?
Well, previously and of course, we did subscribing and there was a video on how to scrape the currency
rates exchange rate from a website called Excretes.
And I do have the code for that, so I'm going to paste it in between here.
So this is the codes I just pasted, and we use beautiful soup, which is a whip scraping library and
requests.
So this function, what it does is it gets as inputs of the inputs, currency and the ultimate currency.
So the two currencies to exchange from one to another.
This is a unit of this website.
So its points.
So this websites and.
If you go to the currency calculator and you choose to currency this year, for example, euro to JPY,
Vural changes, so euro to JPY and then we scrape this value now from that's the way you describe that
is by loading the contents of this of that web page using request that gets and the euro.
We pass it there.
So we get the contents and then we pass it with beautiful soup and then we find the H2 elements for
that particular text so that no, I explain to you how to do this in that video.
So I'm not going to go into details again here.
And basically, we return of that number, so that number will be returned by.
This gets currency function.
So now what we do is we go to the API function that we created in here and then we create a variable
rate is equal to get currency.
So we call that function.
No gets currency will get as far as inputs in coeur.
So that's value.
And at.
So that means the user passes USD and JPY in here.
We get those in here and in here, and then they are passed in the function.
And then from there they go in here.
So USD and JPY.
As argument families to call that function gets currency, so we get the rate for that particular exchange
and then what do we do with the rates?
Well, we will create a function, let's say a result
dictionary and let's create the dictionary like this.
Let's tell the user what inputs currency they entered, for example, which will be incur.
So this is a string, a static string, and this is a variable.
This will hold the value that the user enters you as the all you or JPY.
And also, let's tell them what outputs currency they choose.
So a string and a column and odds occur.
And finally, let's tell them the rates.
So rate a string and the rate as the variable, which holds the actual the rate.
And finally return.
Now we need to import something here from flask import Jason yify.
So this will convert this dictionary into JSON format, which is acceptable by flasks.
So only fine.
And then here you pass a result dictionary.
So let's run it and see if everything looks OK.
Right, so that's the home page.
I'll open it on my browser.
So that's the homepage.
Now let's try to append that part to the domain.
So after the domain, it will be slash API V1.
Use the euro enter.
And that's the results.
So that's the screen, the static string you as the is decided by the user outfits currency another
stream euro is the other exchange that the user enters the year.
And then finally, the rate.
So it's working.
And here you have the code's pretty short codes for what it does.
So again, what's happening is that the service now is on line is public.
In my case, at least because I use Ripple and I passed the horse as zero zero zero zero here so that
Ripple deploys it automatically and it gives us that domain.
And so as a public website.
And then in the home page, we have some basic documentation how the user can access our API.
So then they enter that in here and those USD and euro go inside.
That's, you know, the URL passes.
I mean, here in the function, the function gets, the data from this gets currency function and it
gives us the rates which we serve it through a dictionary.
So using a dictionary is a good practice to serve these kinds of data through APIs.
In other words, adjacent formats, which is similar to a dictionary and no other applications can use
our API, can query URLs to our own API and then get this dictionary.
And then they may process it.
They can do whatever they want afterwards.
So thank you for following and see you later.