WEBVTT 1 00:00:00.630 --> 00:00:02.410 In this video, we're going to be creating 2 00:00:02.410 --> 00:00:05.592 our first programme that interacts with an API. 3 00:00:05.592 --> 00:00:10.150 We're gonna need to instal the requests package, 4 00:00:10.150 --> 00:00:12.470 so go over to your project interpreter 5 00:00:12.470 --> 00:00:13.720 and press the plus icon 6 00:00:13.720 --> 00:00:15.880 and instal the requests package. 7 00:00:15.880 --> 00:00:17.100 If you've got it installed already, 8 00:00:17.100 --> 00:00:18.880 because we used it in the past, 9 00:00:18.880 --> 00:00:21.130 then that's fine, you can just keep that one 10 00:00:21.130 --> 00:00:22.850 but I have created a new project, 11 00:00:22.850 --> 00:00:24.793 so I have to instal it again. 12 00:00:26.350 --> 00:00:27.290 Once that's installed, 13 00:00:27.290 --> 00:00:29.170 just close that and press OK 14 00:00:29.170 --> 00:00:31.890 and that's everything we'll need for this section. 15 00:00:31.890 --> 00:00:36.080 So, let's go over and create our new file 16 00:00:36.080 --> 00:00:39.586 which is gonna be called app.py as usual. 17 00:00:39.586 --> 00:00:42.870 Now, we're going to need to import requests 18 00:00:42.870 --> 00:00:47.050 and we will need to get our APP_ID in here, 19 00:00:47.050 --> 00:00:49.390 so just copy what you had in your dashboard 20 00:00:49.390 --> 00:00:51.160 in open exchange rates. 21 00:00:51.160 --> 00:00:53.460 Next up, we need to go and look 22 00:00:53.460 --> 00:00:55.840 at the open exchange rates documentation 23 00:00:55.840 --> 00:00:57.970 to see what they can give us, 24 00:00:57.970 --> 00:01:00.840 so if you go over to openexchangerates.org 25 00:01:00.840 --> 00:01:03.320 and click on the documentation, 26 00:01:03.320 --> 00:01:06.180 you will see the different parts of the API 27 00:01:06.180 --> 00:01:07.750 that you can access. 28 00:01:07.750 --> 00:01:08.740 You can read through this 29 00:01:08.740 --> 00:01:10.750 and it's actually quite good documentation 30 00:01:10.750 --> 00:01:11.820 but normally what you'll see 31 00:01:11.820 --> 00:01:15.100 is a section called API endpoints. 32 00:01:15.100 --> 00:01:17.290 So, these are the different things that you can access 33 00:01:17.290 --> 00:01:19.110 in this API. 34 00:01:19.110 --> 00:01:21.150 Essentially this is the data 35 00:01:21.150 --> 00:01:23.150 that the API needs from you 36 00:01:23.150 --> 00:01:26.180 in order to know what to give you back. 37 00:01:26.180 --> 00:01:30.287 If we make a request to this address here, 38 00:01:30.287 --> 00:01:32.460 then the API will know 39 00:01:32.460 --> 00:01:36.530 that what we want is the latest exchange rates available 40 00:01:36.530 --> 00:01:38.000 from the API. 41 00:01:38.000 --> 00:01:41.860 So, it will return you those latest exchange rates. 42 00:01:41.860 --> 00:01:44.776 This is what an API is. 43 00:01:44.776 --> 00:01:47.250 It has a definition of what it accepts, 44 00:01:47.250 --> 00:01:49.550 in this case, a request on this endpoint 45 00:01:49.550 --> 00:01:51.710 and a definition on what it responds with. 46 00:01:51.710 --> 00:01:53.750 And that is really what an API is. 47 00:01:53.750 --> 00:01:56.440 So, if you make a request like this 48 00:01:56.440 --> 00:01:59.660 to /api/latest.json, 49 00:01:59.660 --> 00:02:02.930 then it will give you a few different fields 50 00:02:02.930 --> 00:02:07.023 and all the rates that it currently has. 51 00:02:07.970 --> 00:02:10.180 The base rate is always United States dollars, 52 00:02:10.180 --> 00:02:12.530 at least in the free plan, 53 00:02:12.530 --> 00:02:15.963 so you'll get all the rates parting from USD. 54 00:02:16.820 --> 00:02:20.240 This ?app_id=YOUR_APP_ID 55 00:02:20.240 --> 00:02:22.710 is called a query string parameter. 56 00:02:22.710 --> 00:02:25.300 This is how we sent extra information 57 00:02:25.300 --> 00:02:28.410 to APIs and to websites over the internet. 58 00:02:28.410 --> 00:02:29.660 Just put a question mark 59 00:02:29.660 --> 00:02:32.420 and then the key which is app_id 60 00:02:32.420 --> 00:02:34.660 equal and then the value which is the value 61 00:02:34.660 --> 00:02:37.570 of your app ID, so knowing this, 62 00:02:37.570 --> 00:02:39.830 let's copy all of that here 63 00:02:39.830 --> 00:02:40.960 and go back to PyCharm 64 00:02:42.410 --> 00:02:45.903 and we're gonna say that the ENDPOINT is this one there. 65 00:02:47.150 --> 00:02:48.870 Now we're gonna make a request, 66 00:02:48.870 --> 00:02:53.240 so we'll say response equals requests.get 67 00:02:53.240 --> 00:02:56.577 and we will go for the ENDPOINT ?app_id=APP_ID 68 00:02:59.720 --> 00:03:02.820 and this builds out the address like we had 69 00:03:02.820 --> 00:03:06.880 on the documentation and then the exchange_rates 70 00:03:08.670 --> 00:03:10.290 will be the response.json 71 00:03:12.090 --> 00:03:16.820 and we will access the rates value inside this dictionary. 72 00:03:16.820 --> 00:03:19.420 Response.json gives us a Python dictionary 73 00:03:19.420 --> 00:03:21.610 containing the response 74 00:03:21.610 --> 00:03:24.670 and as you can see from here, 75 00:03:24.670 --> 00:03:28.410 this dictionary has a key called rates. 76 00:03:28.410 --> 00:03:33.033 So, we're accessing the rates key inside that response. 77 00:03:34.650 --> 00:03:39.577 Next, let's try to convert $1,000 into pounds. 78 00:03:41.520 --> 00:03:43.860 So, we will say the usd_amount 79 00:03:43.860 --> 00:03:48.510 multiplied by the exchange_rates GBP. 80 00:03:48.510 --> 00:03:51.330 Remember that the base rate of this response 81 00:03:51.330 --> 00:03:53.080 is United States dollars 82 00:03:53.080 --> 00:03:55.660 and that means that the GBP rates 83 00:03:55.660 --> 00:03:58.582 inside this response is the equivalent 84 00:03:58.582 --> 00:04:03.582 of $1, so by multiplying $1,000 by the rate, 85 00:04:03.590 --> 00:04:06.430 you get something like 800 something pounds 86 00:04:06.430 --> 00:04:09.750 and then we can print that USD usd_amount 87 00:04:12.290 --> 00:04:14.583 is GBP gbp_amount. 88 00:04:16.240 --> 00:04:18.133 Let's right click that and run it. 89 00:04:19.410 --> 00:04:20.380 And you see down here 90 00:04:20.380 --> 00:04:22.283 that you get USD1000 is GBP791.2. 91 00:04:26.640 --> 00:04:28.670 Sometimes this number can get really long 92 00:04:28.670 --> 00:04:31.450 depending on the division and how Python does it. 93 00:04:31.450 --> 00:04:32.380 Don't worry too much about it, 94 00:04:32.380 --> 00:04:34.660 we'll learn how to format this 95 00:04:34.660 --> 00:04:36.430 to a certain number of decimal places 96 00:04:36.430 --> 00:04:37.970 in a later video. 97 00:04:37.970 --> 00:04:42.170 So, this is how you interact with an API. 98 00:04:42.170 --> 00:04:44.640 Usually the API requires some form 99 00:04:44.640 --> 00:04:47.290 of authentication or authorization. 100 00:04:47.290 --> 00:04:51.250 In this case, the APP_ID is our form of authorization, 101 00:04:51.250 --> 00:04:53.000 it's just a string that we include 102 00:04:53.000 --> 00:04:55.890 and that tells the API that we are a valid user 103 00:04:55.890 --> 00:04:57.003 when they receive it. 104 00:04:57.900 --> 00:05:00.980 Then it also have a set of data that it requires. 105 00:05:00.980 --> 00:05:03.050 In this case, all the data it requires 106 00:05:03.050 --> 00:05:05.450 is the endpoint that we wanna access 107 00:05:05.450 --> 00:05:07.050 and the app_id 108 00:05:07.050 --> 00:05:08.700 and then it gives us back some data 109 00:05:08.700 --> 00:05:10.180 that is already predetermined 110 00:05:10.180 --> 00:05:12.560 and is always in the documentation or should be 111 00:05:12.560 --> 00:05:15.780 in the documentation and that is the rates. 112 00:05:15.780 --> 00:05:16.980 So, once you have the rates, 113 00:05:16.980 --> 00:05:19.730 you can multiple the usd_amount by that 114 00:05:19.730 --> 00:05:22.733 and get a pound amount or indeed, any other currency. 115 00:05:24.090 --> 00:05:25.470 Thanks for joining me in this video, 116 00:05:25.470 --> 00:05:26.920 I'll see you in the next one.