-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntroduction-to-API.html
More file actions
1162 lines (1131 loc) · 118 KB
/
Introduction-to-API.html
File metadata and controls
1162 lines (1131 loc) · 118 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.2.335">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Ogundepo Ezekiel Adebayo">
<meta name="dcterms.date" content="2023-03-28">
<meta name="description" content="This API tutorial will teach you how to fetch data from an external source using HTTP requests and parse the data into a usable format.">
<title>How to fetch API data in R and Python</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="Introduction-to-API_files/libs/clipboard/clipboard.min.js"></script>
<script src="Introduction-to-API_files/libs/quarto-html/quarto.js"></script>
<script src="Introduction-to-API_files/libs/quarto-html/popper.min.js"></script>
<script src="Introduction-to-API_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="Introduction-to-API_files/libs/quarto-html/anchor.min.js"></script>
<link href="Introduction-to-API_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="Introduction-to-API_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="Introduction-to-API_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="Introduction-to-API_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="Introduction-to-API_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<style>html{ scroll-behavior: smooth; }</style>
<link href="Introduction-to-API_files/libs/pagedtable-1.1/css/pagedtable.css" rel="stylesheet">
<script src="Introduction-to-API_files/libs/pagedtable-1.1/js/pagedtable.js"></script>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-full toc-left">
<div id="quarto-sidebar-toc-left" class="sidebar toc-left">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">On this page</h2>
<ul>
<li><a href="#what-is-an-api" id="toc-what-is-an-api" class="nav-link active" data-scroll-target="#what-is-an-api"><span class="toc-section-number">1</span> What is an API?</a></li>
<li><a href="#type-of-request-methods" id="toc-type-of-request-methods" class="nav-link" data-scroll-target="#type-of-request-methods"><span class="toc-section-number">2</span> Type of request methods</a></li>
<li><a href="#general-rules-for-using-an-api" id="toc-general-rules-for-using-an-api" class="nav-link" data-scroll-target="#general-rules-for-using-an-api"><span class="toc-section-number">3</span> General rules for using an API</a></li>
<li><a href="#how-to-fetch-api-data-using-a-programming-language" id="toc-how-to-fetch-api-data-using-a-programming-language" class="nav-link" data-scroll-target="#how-to-fetch-api-data-using-a-programming-language"><span class="toc-section-number">4</span> How to fetch API data using a programming language</a>
<ul class="collapse">
<li><a href="#sec-api-r" id="toc-sec-api-r" class="nav-link" data-scroll-target="#sec-api-r"><span class="toc-section-number">4.1</span> with R</a></li>
<li><a href="#sec-api-py" id="toc-sec-api-py" class="nav-link" data-scroll-target="#sec-api-py"><span class="toc-section-number">4.2</span> with Python</a></li>
</ul></li>
<li><a href="#practical-example-in-r-and-python" id="toc-practical-example-in-r-and-python" class="nav-link" data-scroll-target="#practical-example-in-r-and-python"><span class="toc-section-number">5</span> Practical example in R and Python</a>
<ul class="collapse">
<li><a href="#without-the-key" id="toc-without-the-key" class="nav-link" data-scroll-target="#without-the-key"><span class="toc-section-number">5.1</span> Without the key</a></li>
<li><a href="#with-the-key" id="toc-with-the-key" class="nav-link" data-scroll-target="#with-the-key"><span class="toc-section-number">5.2</span> With the key</a></li>
</ul></li>
<li><a href="#other-resources" id="toc-other-resources" class="nav-link" data-scroll-target="#other-resources"><span class="toc-section-number">6</span> Other resources</a></li>
</ul>
</nav>
</div>
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar zindex-bottom">
</div>
<main class="content column-page-right" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<div class="quarto-title-block"><div><h1 class="title">How to fetch API data in R and Python</h1><button type="button" class="btn code-tools-button" id="quarto-code-tools-source"><i class="bi"></i> Code</button></div></div>
</div>
<div>
<div class="description">
<p>This API tutorial will teach you how to fetch data from an external source using HTTP requests and parse the data into a usable format.</p>
</div>
</div>
<div class="quarto-title-meta-author">
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-heading">Affiliation</div>
<div class="quarto-title-meta-contents">
<a href="https://bit.ly/gbganalyst">Ogundepo Ezekiel Adebayo</a>
</div>
<div class="quarto-title-meta-contents">
<p class="affiliation">
<a href="https://54gene.com">
54gene
</a>
</p>
</div>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">March 28, 2023</p>
</div>
</div>
</div>
</header>
<div class="callout-note callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>This article is part of my contribution to open-source and for the love of the data community. I hope it will be useful to you and your work.</p>
</div>
</div>
<section id="what-is-an-api" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="what-is-an-api"><span class="header-section-number">1</span> What is an API?</h2>
<div class="quarto-figure quarto-figure-center" style="float:right;">
<figure class="figure">
<p><img src="images/API.jpg" class="img-fluid figure-img" alt="API image" width="200"></p>
<p></p><figcaption class="figure-caption">Image source: Unsplash</figcaption><p></p>
</figure>
</div>
<p>An API (Application Programming Interface) is a set of protocols, routines, and tools used for building software applications. It is essentially a set of rules and methods that data analyst or software developers can use to interact with and access the services and features provided by another application, service or platform.</p>
<p>In simpler terms, an API allows different software applications to communicate with each other and share data in a standardized way. With APIs, developers or analysts can get data without needing to scrape the website, manually download it, or directly go to the company from which they need it.</p>
<div id="fig-elephant" class="quarto-figure quarto-figure-left anchored">
<figure class="figure">
<p><img src="images/how-api-work.jpg" class="img-fluid figure-img" style="width:70.0%"></p>
<p></p><figcaption class="figure-caption">Figure 1: How API work</figcaption><p></p>
</figure>
</div>
<p>For example, if you want to integrate a weather forecast feature into your app, you can use a weather API that provides the necessary data, rather than building the entire feature from scratch. This allows you to focus on the unique aspects of your app, without worrying about the underlying functionality.</p>
</section>
<section id="type-of-request-methods" class="level2" data-number="2">
<h2 data-number="2" class="anchored" data-anchor-id="type-of-request-methods"><span class="header-section-number">2</span> Type of request methods</h2>
<div class="cell" data-layout-align="left">
<div class="cell-output-display">
<div id="fig-api-method" class="quarto-figure quarto-figure-left anchored">
<figure class="figure">
<p><img src="images/image-849919650.png" class="img-fluid figure-img" width="488"></p>
<p></p><figcaption class="figure-caption">Figure 2: Types of request method</figcaption><p></p>
</figure>
</div>
</div>
</div>
<p>As shown in <a href="#fig-api-method">Figure 2</a>, the most common type of API request method is <code>GET</code>.</p>
</section>
<section id="general-rules-for-using-an-api" class="level2" data-number="3">
<h2 data-number="3" class="anchored" data-anchor-id="general-rules-for-using-an-api"><span class="header-section-number">3</span> General rules for using an API</h2>
<p>To use an API to extract data, you will need to follow these steps:</p>
<ol type="1">
<li><p>Find an API that provides the data you are interested in. This may involve doing some research online to find available APIs.</p></li>
<li><p>Familiarize yourself with the API’s documentation to understand how to make requests and what data is available.</p></li>
<li><p>Use a programming language to write a script that sends a request to the API and receives the data. Depending on the API, you may need to include authentication parameters in the request to specify the data you want to receive.</p></li>
<li><p>Parse the data you receive from the API to extract the information you are interested in. The format of the data will depend on the API you are using, and may be in JSON, XML, or another format.</p></li>
<li><p>Process the extracted data in your script, or save it to a file or database for later analysis.</p></li>
</ol>
</section>
<section id="how-to-fetch-api-data-using-a-programming-language" class="level2" data-number="4">
<h2 data-number="4" class="anchored" data-anchor-id="how-to-fetch-api-data-using-a-programming-language"><span class="header-section-number">4</span> How to fetch API data using a programming language</h2>
<section id="sec-api-r" class="level3" data-number="4.1">
<h3 data-number="4.1" class="anchored" data-anchor-id="sec-api-r"><span class="header-section-number">4.1</span> with R</h3>
<p>There are several packages available in R for consuming APIs. Some of the most commonly used packages are:</p>
<ol type="1">
<li><p><strong>httr</strong>: This package provides convenient functions for making HTTP requests and processing the responses.</p></li>
<li><p><strong>jsonlite</strong>: This package is used for parsing JSON data, which is a common format for API responses.</p></li>
<li><p><strong>RCurl</strong>: This package is a wrapper around the libcurl library, which is a powerful and versatile HTTP client library.</p></li>
</ol>
<p>To get data from an API in R, you need to follow these steps:</p>
<ol type="1">
<li>Install the required packages by running the following command in the R console:</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="fu">c</span>(<span class="st">"httr"</span>, <span class="st">"jsonlite"</span>, <span class="st">"RCurl"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="2" type="1">
<li>Load the packages by running the following command:</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(httr)</span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(jsonlite)</span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(RCurl)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="3" type="1">
<li>Make an API request by using the GET function from the httr package. The API endpoint should be passed as an argument to this function.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>response <span class="ot"><-</span> <span class="fu">GET</span>(<span class="st">"https://api.example.com/endpoint"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="4" type="1">
<li>Check the status code of the response to see if the request was successful. A status code of <code>200</code> indicates a successful request.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>status_code <span class="ot"><-</span> <span class="fu">status_code</span>(response)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol start="5" type="1">
<li>Extract the data from the response. If the API returns data in JSON format, you can use the <code>fromJSON</code> function from the <code>jsonlite</code> package to parse the data. Store the data in a variable for later use.</li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>api_data <span class="ot"><-</span> <span class="fu">fromJSON</span>(<span class="fu">content</span>(response, <span class="at">as =</span> <span class="st">"text"</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>These are the basic steps to get data from an API in R. Depending on the API, you may need to pass additional parameters or authentication information in your request. For example,</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>response <span class="ot"><-</span> <span class="fu">GET</span>(</span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="st">"https://api.example.com/endpoint"</span>,</span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">authenticate</span>(</span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="at">user =</span> <span class="st">"API_KEY_HERE"</span>,</span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="at">password =</span> <span class="st">"API_PASSWORD_HERE"</span>,</span>
<span id="cb6-6"><a href="#cb6-6" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"basic"</span></span>
<span id="cb6-7"><a href="#cb6-7" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb6-8"><a href="#cb6-8" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="sec-api-py" class="level3" data-number="4.2">
<h3 data-number="4.2" class="anchored" data-anchor-id="sec-api-py"><span class="header-section-number">4.2</span> with Python</h3>
<p>To use an API in Python, you can use a library such as <code>requests</code> or <code>urllib</code> to send HTTP requests to the API and receive responses. Here’s an example of how to use an API in Python using the requests library:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> requests</span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Define the API endpoint URL and parameters</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a>endpoint <span class="op">=</span> <span class="st">'https://api.example.com/data'</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>params <span class="op">=</span> {<span class="st">'param1'</span>: <span class="st">'value1'</span>, <span class="st">'param2'</span>: <span class="st">'value2'</span>}</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Send a GET request to the API endpoint</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>response <span class="op">=</span> requests.get(endpoint, params <span class="op">=</span> params)</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a><span class="co"># Check if the request was successful</span></span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> response.status_code <span class="op">==</span> <span class="dv">200</span>:</span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="co"># Parse the response JSON data</span></span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> data <span class="op">=</span> response.json()</span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="co"># Process the data, for example by printing it to the console</span></span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(data)</span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span>:</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f'Error: </span><span class="sc">{</span>response<span class="sc">.</span>status_code<span class="sc">}</span><span class="ss">'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>In this example, we’re using the <code>requests</code> library to send a GET request to an API endpoint at <code>https://api.example.com/data</code>, passing two parameters (<code>param1</code> and <code>param2</code>) in the request. The <code>requests.get()</code> method returns a <code>Response</code> object, which we can use to check the response status code and parse the response data.</p>
<p>If the status code is <code>200</code>, we can assume the request was successful, and we can parse the response data using the <code>response.json()</code> method, which converts the JSON-formatted response to a Python object. We can then process the data as needed, for example by printing it to the console.</p>
<p>Of course, the exact API endpoint and parameters will depend on the specific API that you are using, and you’ll need to consult the API documentation to learn how to construct your request correctly. But this example should give you a sense of the general process involved in using an API in Python.</p>
</section>
</section>
<section id="practical-example-in-r-and-python" class="level2" data-number="5">
<h2 data-number="5" class="anchored" data-anchor-id="practical-example-in-r-and-python"><span class="header-section-number">5</span> Practical example in R and Python</h2>
<p>We will use R and Python to fetch the API data without and with the key.</p>
<section id="without-the-key" class="level3" data-number="5.1">
<h3 data-number="5.1" class="anchored" data-anchor-id="without-the-key"><span class="header-section-number">5.1</span> Without the key</h3>
<p>In this example, we will use an API from a site called <a href="https://www.givefood.org.uk/api/2/docs/"><img src="images/give_food.png" height="30"></a> that uses an API without an API key. In this case, we will be using a <code>GET</code> request to fetch the API data at the <strong>food banks</strong> using this link: <a href="https://www.givefood.org.uk/api/2/foodbanks" class="uri">https://www.givefood.org.uk/api/2/foodbanks</a>. Please follow the steps in <a href="#sec-api-r">Section 4.1</a> for R and <a href="#sec-api-py">Section 4.2</a> for Python.</p>
<div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs" role="tablist"><li class="nav-item" role="presentation"><a class="nav-link active" id="tabset-1-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-1" role="tab" aria-controls="tabset-1-1" aria-selected="true" href="">R</a></li><li class="nav-item" role="presentation"><a class="nav-link" id="tabset-1-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-1-2" role="tab" aria-controls="tabset-1-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-1-1" class="tab-pane active" role="tabpanel" aria-labelledby="tabset-1-1-tab">
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(httr)</span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(jsonlite)</span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a>response <span class="ot"><-</span> <span class="fu">GET</span>(<span class="st">"https://www.givefood.org.uk/api/2/foodbanks"</span>)</span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb8-9"><a href="#cb8-9" aria-hidden="true" tabindex="-1"></a><span class="fu">status_code</span>(response)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> [1] 200</code></pre>
</div>
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="ot"><-</span> <span class="fu">fromJSON</span>(<span class="fu">content</span>(response, <span class="at">as =</span> <span class="st">"text"</span>), <span class="at">flatten =</span> <span class="cn">TRUE</span>)</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="sc">%>%</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">dim</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> [1] 834 27</code></pre>
</div>
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="sc">%>%</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
<script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["name"],"name":[1],"type":["chr"],"align":["left"]},{"label":["alt_name"],"name":[2],"type":["chr"],"align":["left"]},{"label":["slug"],"name":[3],"type":["chr"],"align":["left"]},{"label":["phone"],"name":[4],"type":["chr"],"align":["left"]},{"label":["secondary_phone"],"name":[5],"type":["chr"],"align":["left"]},{"label":["email"],"name":[6],"type":["chr"],"align":["left"]},{"label":["address"],"name":[7],"type":["chr"],"align":["left"]},{"label":["postcode"],"name":[8],"type":["chr"],"align":["left"]},{"label":["closed"],"name":[9],"type":["lgl"],"align":["right"]},{"label":["country"],"name":[10],"type":["chr"],"align":["left"]},{"label":["lat_lng"],"name":[11],"type":["chr"],"align":["left"]},{"label":["network"],"name":[12],"type":["chr"],"align":["left"]},{"label":["created"],"name":[13],"type":["chr"],"align":["left"]},{"label":["urls.self"],"name":[14],"type":["chr"],"align":["left"]},{"label":["urls.html"],"name":[15],"type":["chr"],"align":["left"]},{"label":["urls.homepage"],"name":[16],"type":["chr"],"align":["left"]},{"label":["urls.shopping_list"],"name":[17],"type":["chr"],"align":["left"]},{"label":["charity.registration_id"],"name":[18],"type":["chr"],"align":["left"]},{"label":["charity.register_url"],"name":[19],"type":["chr"],"align":["left"]},{"label":["politics.parliamentary_constituency"],"name":[20],"type":["chr"],"align":["left"]},{"label":["politics.mp"],"name":[21],"type":["chr"],"align":["left"]},{"label":["politics.mp_party"],"name":[22],"type":["chr"],"align":["left"]},{"label":["politics.mp_parl_id"],"name":[23],"type":["int"],"align":["right"]},{"label":["politics.ward"],"name":[24],"type":["chr"],"align":["left"]},{"label":["politics.district"],"name":[25],"type":["chr"],"align":["left"]},{"label":["politics.urls.self"],"name":[26],"type":["chr"],"align":["left"]},{"label":["politics.urls.html"],"name":[27],"type":["chr"],"align":["left"]}],"data":[{"1":"Llangollen","2":"NA","3":"llangollen","4":"07835261343","5":"NA","6":"llangollenfoodshare@gmail.com","7":"36c Castle Street\\r\\nLlangollen\\r\\nLL20 8RT","8":"LL20 8RT","9":"FALSE","10":"Wales","11":"52.9693817,-3.1705863","12":"Independent","13":"2023-03-22T11:21:07.335","14":"https://www.givefood.org.uk/api/2/foodbank/llangollen/","15":"https://www.givefood.org.uk/needs/at/llangollen/","16":"https://llangollen-food-share.business.site/","17":"https://llangollen-food-share.business.site/","18":"NA","19":"NA","20":"Clwyd South","21":"Simon Baynes","22":"Conservative","23":"4800","24":"Llangollen","25":"Denbighshire","26":"https://www.givefood.org.uk/api/2/constituency/clwyd-south/","27":"https://www.givefood.org.uk/needs/in/constituency/clwyd-south/","_rn_":"1"},{"1":"Carterton Community","2":"NA","3":"carterton-community","4":"NA","5":"NA","6":"hello@cartertonfoodbank.co.uk","7":"Burford Road\\r\\nCarterton\\r\\nOX18 3AG","8":"OX18 3AG","9":"FALSE","10":"England","11":"51.7605753,-1.5950399","12":"Independent","13":"2023-03-21T08:47:47.679","14":"https://www.givefood.org.uk/api/2/foodbank/carterton-community/","15":"https://www.givefood.org.uk/needs/at/carterton-community/","16":"https://cartertonfoodbank.co.uk","17":"https://cartertonfoodbank.co.uk/can-you-help","18":"NA","19":"NA","20":"Witney","21":"Robert Courts","22":"Conservative","23":"4589","24":"Carterton North West","25":"West Oxfordshire","26":"https://www.givefood.org.uk/api/2/constituency/witney/","27":"https://www.givefood.org.uk/needs/in/constituency/witney/","_rn_":"2"},{"1":"A Helping Hand In The Highlands","2":"NA","3":"a-helping-hand-in-the-highlands","4":"07380343850","5":"NA","6":"ahhith1@gmail.com","7":"124 Milnafua\\r\\nAlness\\r\\nIV17 0YT","8":"IV17 0YT","9":"FALSE","10":"Scotland","11":"57.70058040000001,-4.2285586","12":"NA","13":"2023-03-07T08:50:29.172","14":"https://www.givefood.org.uk/api/2/foodbank/a-helping-hand-in-the-highlands/","15":"https://www.givefood.org.uk/needs/at/a-helping-hand-in-the-highlands/","16":"https://a-helping-hand-in-the-highlands.business.site","17":"https://a-helping-hand-in-the-highlands.business.site","18":"NA","19":"NA","20":"Caithness, Sutherland and Easter Ross","21":"Jamie Stone","22":"Liberal Democrats","23":"4612","24":"Cromarty Firth","25":"Highland","26":"https://www.givefood.org.uk/api/2/constituency/caithness-sutherland-and-easter-ross/","27":"https://www.givefood.org.uk/needs/in/constituency/caithness-sutherland-and-easter-ross/","_rn_":"3"},{"1":"Breaking Bread","2":"NA","3":"breaking-bread","4":"07936744177","5":"NA","6":"breakingbreadfoodbank@gmail.com","7":"93/94 Walsall Street\\r\\nWednesbury\\r\\nWS10 9BY","8":"WS10 9BY","9":"FALSE","10":"England","11":"52.5534607,-2.0177951","12":"Independent","13":"2023-03-19T11:36:57.499","14":"https://www.givefood.org.uk/api/2/foodbank/breaking-bread/","15":"https://www.givefood.org.uk/needs/at/breaking-bread/","16":"https://www.breakingbreadfoodbank.co.uk","17":"https://www.breakingbreadfoodbank.co.uk","18":"NA","19":"NA","20":"West Bromwich West","21":"Shaun Bailey","22":"Conservative","23":"4757","24":"Wednesbury North","25":"Sandwell","26":"https://www.givefood.org.uk/api/2/constituency/west-bromwich-west/","27":"https://www.givefood.org.uk/needs/in/constituency/west-bromwich-west/","_rn_":"4"},{"1":"Breadline Project","2":"NA","3":"breadline-project","4":"07411602448","5":"NA","6":"breadline@outlook.com","7":"4 Cherry Road\\r\\nBanbury\\r\\nOX16 0RL","8":"OX16 0RL","9":"FALSE","10":"England","11":"52.0661129,-1.3586409","12":"Independent","13":"2022-04-07T19:19:18.022","14":"https://www.givefood.org.uk/api/2/foodbank/breadline-project/","15":"https://www.givefood.org.uk/needs/at/breadline-project/","16":"http://breadlineproject.org","17":"http://breadlineproject.org/Get-Involved/","18":"NA","19":"NA","20":"Banbury","21":"Victoria Prentis","22":"Conservative","23":"4401","24":"Banbury Ruscote","25":"Cherwell","26":"https://www.givefood.org.uk/api/2/constituency/banbury/","27":"https://www.givefood.org.uk/needs/in/constituency/banbury/","_rn_":"5"},{"1":"Durness & Kinlochbervie","2":"NA","3":"durness-kinlochbervie","4":"01971521287","5":"NA","6":"glad.mac@btinternet.com","7":"Church of Scotland Manse\\r\\nManse Road\\r\\nKinlochbervie\\r\\nSutherland\\r\\nIV27 4RG","8":"IV27 4RG","9":"FALSE","10":"Scotland","11":"58.46095649999999,-5.0349578","12":"Independent","13":"2023-01-31T17:21:04.764","14":"https://www.givefood.org.uk/api/2/foodbank/durness-kinlochbervie/","15":"https://www.givefood.org.uk/needs/at/durness-kinlochbervie/","16":"https://dkparish.wordpress.com/kinlochbervie-church-of-scotland/","17":"https://dkparish.wordpress.com/kinlochbervie-church-of-scotland/","18":"SC005079","19":"https://www.oscr.org.uk/about-charities/search-the-register/charity-details?number=SC005079","20":"Caithness, Sutherland and Easter Ross","21":"Jamie Stone","22":"Liberal Democrats","23":"4612","24":"North, West and Central Sutherland","25":"Highland","26":"https://www.givefood.org.uk/api/2/constituency/caithness-sutherland-and-easter-ross/","27":"https://www.givefood.org.uk/needs/in/constituency/caithness-sutherland-and-easter-ross/","_rn_":"6"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
</script>
</div>
</div>
</div>
</div>
<div id="tabset-1-2" class="tab-pane" role="tabpanel" aria-labelledby="tabset-1-2-tab">
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> requests</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a>response <span class="op">=</span> requests.get(<span class="st">"https://www.givefood.org.uk/api/2/foodbanks"</span>)</span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Check if the request was successful</span></span>
<span id="cb13-8"><a href="#cb13-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-9"><a href="#cb13-9" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(response.status_code)</span>
<span id="cb13-10"><a href="#cb13-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-11"><a href="#cb13-11" aria-hidden="true" tabindex="-1"></a><span class="co"># Parse the response JSON data</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> 200</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>food_json <span class="op">=</span> response.json()</span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert to a pandas dataframe</span></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="op">=</span> pd.json_normalize(food_json)</span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a>food_dataframe.shape</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> (834, 27)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>food_dataframe.head()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> name ... politics.urls.html
#> 0 Llangollen ... https://www.givefood.org.uk/needs/in/constitue...
#> 1 Carterton Community ... https://www.givefood.org.uk/needs/in/constitue...
#> 2 A Helping Hand In The Highlands ... https://www.givefood.org.uk/needs/in/constitue...
#> 3 Breaking Bread ... https://www.givefood.org.uk/needs/in/constitue...
#> 4 Breadline Project ... https://www.givefood.org.uk/needs/in/constitue...
#>
#> [5 rows x 27 columns]</code></pre>
</div>
</div>
<p>In this example, we use the <code>pd.json_normalize()</code> method to flatten the list of dictionaries and create a dataframe from it. The resulting dataframe has columns for each key in the JSON objects.</p>
</div>
</div>
</div>
</section>
<section id="with-the-key" class="level3" data-number="5.2">
<h3 data-number="5.2" class="anchored" data-anchor-id="with-the-key"><span class="header-section-number">5.2</span> With the key</h3>
<p>In this example, we will use an API from <a href="https://www.reed.co.uk/developers/Jobseeker"><img src="images/image-874061681.png" width="156" height="30"></a> that uses an API key. In this case, we will use a <code>GET</code> request to fetch data for analyst jobs based in London from the <a href="https://www.reed.co.uk/api/1.0/search?keywords=analyst&location=london&distancefromlocation=15">Jobseeker</a> API. Please follow the steps in <a href="#sec-api-r">Section 4.1</a> for R and <a href="#sec-api-py">Section 4.2</a> for Python, and sign up for the API key at the Jobseeker <a href="https://www.reed.co.uk/developers/Jobseeker">website</a>.</p>
<div class="panel-tabset" data-group="language">
<ul class="nav nav-tabs" role="tablist"><li class="nav-item" role="presentation"><a class="nav-link active" id="tabset-2-1-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-1" role="tab" aria-controls="tabset-2-1" aria-selected="true" href="">R</a></li><li class="nav-item" role="presentation"><a class="nav-link" id="tabset-2-2-tab" data-bs-toggle="tab" data-bs-target="#tabset-2-2" role="tab" aria-controls="tabset-2-2" aria-selected="false" href="">Python</a></li></ul>
<div class="tab-content" data-group="language">
<div id="tabset-2-1" class="tab-pane active" role="tabpanel" aria-labelledby="tabset-2-1-tab">
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(httr)</span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(jsonlite)</span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Create a GET response to call the API</span></span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a>response <span class="ot"><-</span> <span class="fu">GET</span>(</span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a> <span class="st">"https://www.reed.co.uk/api/1.0/search?keywords=analyst&location=london&distancefromlocation=15"</span>,</span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">authenticate</span>(</span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a> <span class="at">user =</span> <span class="fu">Sys.getenv</span>(<span class="st">"putyourapikeyhere"</span>),</span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a> <span class="at">password =</span> <span class="st">""</span></span>
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb19-15"><a href="#cb19-15" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="callout-tip callout callout-style-default callout-captioned">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-caption-container flex-fill">
Tip
</div>
</div>
<div class="callout-body-container callout-body">
<p>Replace <code>Sys.getenv("putyourapikeyhere")</code> with your own API key.</p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">status_code</span>(response)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> [1] 200</code></pre>
</div>
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert the JSON string to a dataframe and view data in a table</span></span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a>job_dataframe <span class="ot"><-</span> <span class="fu">fromJSON</span>(<span class="fu">content</span>(response, <span class="at">as =</span> <span class="st">"text"</span>), <span class="at">flatten =</span> <span class="cn">TRUE</span>)</span>
<span id="cb22-4"><a href="#cb22-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-5"><a href="#cb22-5" aria-hidden="true" tabindex="-1"></a><span class="co"># The job dataframe is inside the results</span></span>
<span id="cb22-6"><a href="#cb22-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb22-7"><a href="#cb22-7" aria-hidden="true" tabindex="-1"></a>job_dataframe<span class="sc">$</span>results <span class="sc">%>%</span></span>
<span id="cb22-8"><a href="#cb22-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">dim</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> [1] 100 15</code></pre>
</div>
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>job_dataframe<span class="sc">$</span>results <span class="sc">%>%</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div data-pagedtable="false">
<script data-pagedtable-source="" type="application/json">
{"columns":[{"label":[""],"name":["_rn_"],"type":[""],"align":["left"]},{"label":["jobId"],"name":[1],"type":["int"],"align":["right"]},{"label":["employerId"],"name":[2],"type":["int"],"align":["right"]},{"label":["employerName"],"name":[3],"type":["chr"],"align":["left"]},{"label":["employerProfileId"],"name":[4],"type":["lgl"],"align":["right"]},{"label":["employerProfileName"],"name":[5],"type":["lgl"],"align":["right"]},{"label":["jobTitle"],"name":[6],"type":["chr"],"align":["left"]},{"label":["locationName"],"name":[7],"type":["chr"],"align":["left"]},{"label":["minimumSalary"],"name":[8],"type":["dbl"],"align":["right"]},{"label":["maximumSalary"],"name":[9],"type":["dbl"],"align":["right"]},{"label":["currency"],"name":[10],"type":["chr"],"align":["left"]},{"label":["expirationDate"],"name":[11],"type":["chr"],"align":["left"]},{"label":["date"],"name":[12],"type":["chr"],"align":["left"]},{"label":["jobDescription"],"name":[13],"type":["chr"],"align":["left"]},{"label":["applications"],"name":[14],"type":["int"],"align":["right"]},{"label":["jobUrl"],"name":[15],"type":["chr"],"align":["left"]}],"data":[{"1":"50108771","2":"593269","3":"Brook Street","4":"NA","5":"NA","6":"Business Support Analyst","7":"London","8":"12.59","9":"12.59","10":"GBP","11":"09/05/2023","12":"28/03/2023","13":"Security Support Contract: August 2023 Salary: £12.59 per hourLocation: Vauxhall, London5 day's work setting:Working pattern: Monday - Friday (8am - 4pm / 10am - 6pm - Flexi time)This is a temporary contract role until August for a Security Support role with an immediate start date (Security Clearance dependant) for an Eleven -month duration with a possible extension offering an office setting a...","14":"0","15":"https://www.reed.co.uk/jobs/business-support-analyst/50108771","_rn_":"1"},{"1":"49848024","2":"184213","3":"2i Recruit Ltd","4":"NA","5":"NA","6":"Business Analyst","7":"Maidenhead","8":"60000.00","9":"70000.00","10":"GBP","11":"09/05/2023","12":"21/02/2023","13":"We have a great opportunity for a Business Analyst who will act as a subject matter expert in business process and controls. The company is a global organisation, sitting in the consumer goods market. This role will be based in the UK office in Maidenhead. There will be opportunities for the successful candidate to support the development of the overall framework, standards, and approach for business process. This wi...","14":"115","15":"https://www.reed.co.uk/jobs/business-analyst/49848024","_rn_":"2"},{"1":"50108748","2":"575264","3":"Reed","4":"NA","5":"NA","6":"Supply Chain Analyst","7":"Stockton-on-Tees","8":"27000.00","9":"30000.00","10":"GBP","11":"28/04/2023","12":"28/03/2023","13":"Reed Procurement are proud to represent a highly successful FMCG business to appoint a Supply Chain Analyst. As a Supply Chain Analyst, you will provide support to enable the Supply Chain to both optimise and control its costs and identify opportunities to increase service to its customers. The ideal candidate will be passionate about producing accurate data which will impact the decision-making within the supply chai...","14":"0","15":"https://www.reed.co.uk/jobs/supply-chain-analyst/50108748","_rn_":"3"},{"1":"50108731","2":"105608","3":"Quality Personnel Services Limited","4":"NA","5":"NA","6":"Logistics Analyst","7":"Blakelands","8":"28000.00","9":"28000.00","10":"GBP","11":"09/05/2023","12":"28/03/2023","13":"We are recruiting a Logistics Analyst to deliver an efficient IT support function as well as playing a key part in providing assistance to the logistics division of a well known global business Hours of work: 9am-5.30pm (some out of hours work needed on a rota basis to provide support) Benefits: Enhanced holiday pay are qualifying periodLife AssuranceAnnual bonusOption to...","14":"0","15":"https://www.reed.co.uk/jobs/logistics-analyst/50108731","_rn_":"4"},{"1":"50108708","2":"390934","3":"Jonathan Lee Recruitment","4":"NA","5":"NA","6":"Strategy Analyst","7":"Peterborough","8":"20.00","9":"25.15","10":"GBP","11":"09/05/2023","12":"28/03/2023","13":"Strategy Analyst Up to £25.15/hr Ltd Umbrella – Inside IR35 Peterborough (hybrid working) 12 month initial contract Job Description: The Sales and Marketing function of the Industrial Power Systems Division is seeking a Strategy Analyst to undertake key reporting and governance, driving focus on our commercial excellen...","14":"0","15":"https://www.reed.co.uk/jobs/strategy-analyst/50108708","_rn_":"5"},{"1":"49801354","2":"323432","3":"BPS World","4":"NA","5":"NA","6":"Business Analyst","7":"Manchester","8":"NA","9":"NA","10":"NA","11":"09/05/2023","12":"15/02/2023","13":"Business Analyst Full-time, permanent Manchester, with expectation to be on client side up to 2 days per week At BPS we are delighted to be a recruiting partner to North Highland - the world’s most innovative and collaborative consulting group. Do you have the skills, passion, and commitment to transform industries for the better? Think you can bring your A-game...","14":"182","15":"https://www.reed.co.uk/jobs/business-analyst/49801354","_rn_":"6"}],"options":{"columns":{"min":{},"max":[10]},"rows":{"min":[10],"max":[10]},"pages":{}}}
</script>
</div>
</div>
</div>
</div>
<div id="tabset-2-2" class="tab-pane" role="tabpanel" aria-labelledby="tabset-2-2-tab">
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> requests</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Set API endpoint and API key</span></span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-7"><a href="#cb25-7" aria-hidden="true" tabindex="-1"></a>url <span class="op">=</span> <span class="st">"https://www.reed.co.uk/api/1.0/search?keywords=analyst&location=london&distancefromlocation=15"</span></span>
<span id="cb25-8"><a href="#cb25-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-9"><a href="#cb25-9" aria-hidden="true" tabindex="-1"></a>api_key <span class="op">=</span> <span class="st">"replace with your own API key"</span> </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Based on the instructions in the API documentation, you will need to include your API key for all requests in a basic authentication http header as the username, leaving the password empty.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Send a GET request to the API endpoint</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a>response <span class="op">=</span> requests.get(url, auth <span class="op">=</span> (api_key, <span class="st">''</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb27"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb27-1"><a href="#cb27-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Check if the request was successful</span></span>
<span id="cb27-2"><a href="#cb27-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-3"><a href="#cb27-3" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(response.status_code)</span>
<span id="cb27-4"><a href="#cb27-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb27-5"><a href="#cb27-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Parse the response JSON data</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> 200</code></pre>
</div>
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a>job_json <span class="op">=</span> response.json()</span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert to a pandas dataframe</span></span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a><span class="co"># The dataframe is inside the results</span></span>
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-7"><a href="#cb29-7" aria-hidden="true" tabindex="-1"></a>job_dataframe <span class="op">=</span> pd.json_normalize(job_json[<span class="st">"results"</span>])</span>
<span id="cb29-8"><a href="#cb29-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb29-9"><a href="#cb29-9" aria-hidden="true" tabindex="-1"></a>job_dataframe.shape</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> (100, 15)</code></pre>
</div>
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a>job_dataframe.head()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>#> jobId ... jobUrl
#> 0 50108771 ... https://www.reed.co.uk/jobs/business-support-a...
#> 1 49848024 ... https://www.reed.co.uk/jobs/business-analyst/4...
#> 2 50108748 ... https://www.reed.co.uk/jobs/supply-chain-analy...
#> 3 50108731 ... https://www.reed.co.uk/jobs/logistics-analyst/...
#> 4 50108708 ... https://www.reed.co.uk/jobs/strategy-analyst/5...
#>
#> [5 rows x 15 columns]</code></pre>
</div>
</div>
</div>
</div>
</div>
<p>You can now use the data for your data science.</p>
</section>
</section>
<section id="other-resources" class="level2" data-number="6">
<h2 data-number="6" class="anchored" data-anchor-id="other-resources"><span class="header-section-number">6</span> Other resources</h2>
<p>You can also watch <code>Dean Chereden</code> YouTube video on how to GET data from an API using R in RStudio.</p>
<div class="quarto-video ratio ratio-16x9"><iframe src="https://www.youtube.com/embed/AhZ42vSmDmE" title="" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe></div>
<hr>
<p>I hope you found this article informative. You can find its GitHub repository <a href="https://github.com/gbganalyst/API-in-R-and-Python">here</a>. If you enjoyed reading this write-up, please follow me on <a href="https://twitter.com/gbganalyst">Twitter</a> and <a href="https://linkedin.com/in/ezekiel-ogundepo">Linkedin</a> for more updates on <code>R</code>, <code>Python</code>, and <code>Excel</code> for data science.</p>
<!-- -->
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
const viewSource = window.document.getElementById('quarto-view-source') ||
window.document.getElementById('quarto-code-tools-source');
if (viewSource) {
const sourceUrl = viewSource.getAttribute("data-quarto-source-url");
viewSource.addEventListener("click", function(e) {
if (sourceUrl) {
// rstudio viewer pane
if (/\bcapabilities=\b/.test(window.location)) {
window.open(sourceUrl);
} else {
window.location.href = sourceUrl;
}
} else {
const modal = new bootstrap.Modal(document.getElementById('quarto-embedded-source-code-modal'));
modal.show();
}
return false;
});
}
function toggleCodeHandler(show) {
return function(e) {
const detailsSrc = window.document.querySelectorAll(".cell > details > .sourceCode");
for (let i=0; i<detailsSrc.length; i++) {
const details = detailsSrc[i].parentElement;
if (show) {
details.open = true;
} else {
details.removeAttribute("open");
}
}
const cellCodeDivs = window.document.querySelectorAll(".cell > .sourceCode");
const fromCls = show ? "hidden" : "unhidden";
const toCls = show ? "unhidden" : "hidden";
for (let i=0; i<cellCodeDivs.length; i++) {
const codeDiv = cellCodeDivs[i];
if (codeDiv.classList.contains(fromCls)) {
codeDiv.classList.remove(fromCls);
codeDiv.classList.add(toCls);
}
}
return false;
}
}
const hideAllCode = window.document.getElementById("quarto-hide-all-code");
if (hideAllCode) {
hideAllCode.addEventListener("click", toggleCodeHandler(false));
}
const showAllCode = window.document.getElementById("quarto-show-all-code");
if (showAllCode) {
showAllCode.addEventListener("click", toggleCodeHandler(true));
}
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var filterRegex = new RegExp('/' + window.location.host + '/');
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// target, if specified
link.setAttribute("target", "_blank");
// default icon
link.classList.add("external");
}
}
});
</script><div class="modal fade" id="quarto-embedded-source-code-modal" tabindex="-1" aria-labelledby="quarto-embedded-source-code-modal-label" aria-hidden="true"><div class="modal-dialog modal-dialog-scrollable"><div class="modal-content"><div class="modal-header"><h5 class="modal-title" id="quarto-embedded-source-code-modal-label">Source Code</h5><button class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><div class="">
<div class="sourceCode" id="cb33" data-shortcodes="false"><pre class="sourceCode markdown code-with-copy"><code class="sourceCode markdown"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a><span class="co">---</span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a><span class="an">title:</span><span class="co"> "How to fetch API data in R and Python"</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a><span class="an">description:</span><span class="co"> | </span></span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a><span class="co"> This API tutorial will teach you how to fetch data from an external source using HTTP requests and parse the data into a usable format.</span></span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a><span class="an">date:</span><span class="co"> "`r format(Sys.time(), '%B %d, %Y')`"</span></span>
<span id="cb33-6"><a href="#cb33-6" aria-hidden="true" tabindex="-1"></a><span class="an">author:</span></span>
<span id="cb33-7"><a href="#cb33-7" aria-hidden="true" tabindex="-1"></a><span class="co"> - name: Ogundepo Ezekiel Adebayo</span></span>
<span id="cb33-8"><a href="#cb33-8" aria-hidden="true" tabindex="-1"></a><span class="co"> url: https://bit.ly/gbganalyst</span></span>
<span id="cb33-9"><a href="#cb33-9" aria-hidden="true" tabindex="-1"></a><span class="co"> affiliation: 54gene</span></span>
<span id="cb33-10"><a href="#cb33-10" aria-hidden="true" tabindex="-1"></a><span class="co"> affiliation-url: https://54gene.com</span></span>
<span id="cb33-11"><a href="#cb33-11" aria-hidden="true" tabindex="-1"></a><span class="an">toc:</span><span class="co"> true</span></span>
<span id="cb33-12"><a href="#cb33-12" aria-hidden="true" tabindex="-1"></a><span class="an">toc-title:</span><span class="co"> "On this page"</span></span>
<span id="cb33-13"><a href="#cb33-13" aria-hidden="true" tabindex="-1"></a><span class="an">toc-location:</span><span class="co"> left</span></span>
<span id="cb33-14"><a href="#cb33-14" aria-hidden="true" tabindex="-1"></a><span class="an">number-sections:</span><span class="co"> true</span></span>
<span id="cb33-15"><a href="#cb33-15" aria-hidden="true" tabindex="-1"></a><span class="an">highlight-style:</span><span class="co"> pygments</span></span>
<span id="cb33-16"><a href="#cb33-16" aria-hidden="true" tabindex="-1"></a><span class="an">format:</span><span class="co"> </span></span>
<span id="cb33-17"><a href="#cb33-17" aria-hidden="true" tabindex="-1"></a><span class="co"> html:</span></span>
<span id="cb33-18"><a href="#cb33-18" aria-hidden="true" tabindex="-1"></a><span class="co"> theme: cosmo</span></span>
<span id="cb33-19"><a href="#cb33-19" aria-hidden="true" tabindex="-1"></a><span class="co"> page-layout: full # custom #article,</span></span>
<span id="cb33-20"><a href="#cb33-20" aria-hidden="true" tabindex="-1"></a><span class="co"> code-fold: false</span></span>
<span id="cb33-21"><a href="#cb33-21" aria-hidden="true" tabindex="-1"></a><span class="co"> code-tools: true</span></span>
<span id="cb33-22"><a href="#cb33-22" aria-hidden="true" tabindex="-1"></a><span class="co"> df-print: paged</span></span>
<span id="cb33-23"><a href="#cb33-23" aria-hidden="true" tabindex="-1"></a><span class="co"> smooth-scroll: true</span></span>
<span id="cb33-24"><a href="#cb33-24" aria-hidden="true" tabindex="-1"></a><span class="co"> link-external-icon: true</span></span>
<span id="cb33-25"><a href="#cb33-25" aria-hidden="true" tabindex="-1"></a><span class="co"> link-external-newwindow: true</span></span>
<span id="cb33-26"><a href="#cb33-26" aria-hidden="true" tabindex="-1"></a><span class="an">editor:</span><span class="co"> visual</span></span>
<span id="cb33-27"><a href="#cb33-27" aria-hidden="true" tabindex="-1"></a><span class="an">execute:</span></span>
<span id="cb33-28"><a href="#cb33-28" aria-hidden="true" tabindex="-1"></a><span class="co"> error: true</span></span>
<span id="cb33-29"><a href="#cb33-29" aria-hidden="true" tabindex="-1"></a><span class="co"> eval: false</span></span>
<span id="cb33-30"><a href="#cb33-30" aria-hidden="true" tabindex="-1"></a><span class="co"> echo: true #fenced</span></span>
<span id="cb33-31"><a href="#cb33-31" aria-hidden="true" tabindex="-1"></a><span class="co"> freeze: auto</span></span>
<span id="cb33-32"><a href="#cb33-32" aria-hidden="true" tabindex="-1"></a><span class="an">knitr:</span></span>
<span id="cb33-33"><a href="#cb33-33" aria-hidden="true" tabindex="-1"></a><span class="co"> opts_chunk:</span></span>
<span id="cb33-34"><a href="#cb33-34" aria-hidden="true" tabindex="-1"></a><span class="co"> comment: "#>"</span></span>
<span id="cb33-35"><a href="#cb33-35" aria-hidden="true" tabindex="-1"></a><span class="co"> #collapse: true</span></span>
<span id="cb33-36"><a href="#cb33-36" aria-hidden="true" tabindex="-1"></a><span class="co"> tidy: 'styler'</span></span>
<span id="cb33-37"><a href="#cb33-37" aria-hidden="true" tabindex="-1"></a><span class="co"> message: false</span></span>
<span id="cb33-38"><a href="#cb33-38" aria-hidden="true" tabindex="-1"></a><span class="co"> warning: false</span></span>
<span id="cb33-39"><a href="#cb33-39" aria-hidden="true" tabindex="-1"></a><span class="co"> wrap: true</span></span>
<span id="cb33-40"><a href="#cb33-40" aria-hidden="true" tabindex="-1"></a><span class="an">editor_options:</span><span class="co"> </span></span>
<span id="cb33-41"><a href="#cb33-41" aria-hidden="true" tabindex="-1"></a><span class="co"> chunk_output_type: console</span></span>
<span id="cb33-42"><a href="#cb33-42" aria-hidden="true" tabindex="-1"></a><span class="co">---</span></span>
<span id="cb33-43"><a href="#cb33-43" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-44"><a href="#cb33-44" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-45"><a href="#cb33-45" aria-hidden="true" tabindex="-1"></a>:::{.callout-note}</span>
<span id="cb33-46"><a href="#cb33-46" aria-hidden="true" tabindex="-1"></a>This article is part of my contribution to open-source and for the love of the data community. I hope it will be useful to you and your work.</span>
<span id="cb33-47"><a href="#cb33-47" aria-hidden="true" tabindex="-1"></a>:::</span>
<span id="cb33-48"><a href="#cb33-48" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-49"><a href="#cb33-49" aria-hidden="true" tabindex="-1"></a><span class="fu">## What is an API?</span></span>
<span id="cb33-50"><a href="#cb33-50" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-51"><a href="#cb33-51" aria-hidden="true" tabindex="-1"></a><span class="al"></span>{style="float:right;" fig-alt="API image" width="200"}</span>
<span id="cb33-52"><a href="#cb33-52" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-53"><a href="#cb33-53" aria-hidden="true" tabindex="-1"></a>An API (Application Programming Interface) is a set of protocols, routines, and tools used for building software applications. It is essentially a set of rules and methods that data analyst or software developers can use to interact with and access the services and features provided by another application, service or platform.</span>
<span id="cb33-54"><a href="#cb33-54" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-55"><a href="#cb33-55" aria-hidden="true" tabindex="-1"></a>In simpler terms, an API allows different software applications to communicate with each other and share data in a standardized way. With APIs, developers or analysts can get data without needing to scrape the website, manually download it, or directly go to the company from which they need it.</span>
<span id="cb33-56"><a href="#cb33-56" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-57"><a href="#cb33-57" aria-hidden="true" tabindex="-1"></a><span class="al"></span>{#fig-elephant fig-align="left" width="70%"}</span>
<span id="cb33-58"><a href="#cb33-58" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-59"><a href="#cb33-59" aria-hidden="true" tabindex="-1"></a>For example, if you want to integrate a weather forecast feature into your app, you can use a weather API that provides the necessary data, rather than building the entire feature from scratch. This allows you to focus on the unique aspects of your app, without worrying about the underlying functionality.</span>
<span id="cb33-60"><a href="#cb33-60" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-61"><a href="#cb33-61" aria-hidden="true" tabindex="-1"></a><span class="fu">## Type of request methods</span></span>
<span id="cb33-62"><a href="#cb33-62" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-65"><a href="#cb33-65" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-66"><a href="#cb33-66" aria-hidden="true" tabindex="-1"></a><span class="co">#| label: fig-api-method</span></span>
<span id="cb33-67"><a href="#cb33-67" aria-hidden="true" tabindex="-1"></a><span class="co">#| fig-cap: Types of request method</span></span>
<span id="cb33-68"><a href="#cb33-68" aria-hidden="true" tabindex="-1"></a><span class="co">#| fig-align: left</span></span>
<span id="cb33-69"><a href="#cb33-69" aria-hidden="true" tabindex="-1"></a><span class="co">#| eval: true</span></span>
<span id="cb33-70"><a href="#cb33-70" aria-hidden="true" tabindex="-1"></a><span class="co">#| echo: false</span></span>
<span id="cb33-71"><a href="#cb33-71" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-72"><a href="#cb33-72" aria-hidden="true" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">include_graphics</span>(<span class="st">"images/image-849919650.png"</span>)</span>
<span id="cb33-73"><a href="#cb33-73" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-74"><a href="#cb33-74" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(reticulate)</span>
<span id="cb33-75"><a href="#cb33-75" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-76"><a href="#cb33-76" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-77"><a href="#cb33-77" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-78"><a href="#cb33-78" aria-hidden="true" tabindex="-1"></a>As shown in @fig-api-method, the most common type of API request method is <span class="in">`GET`</span>.</span>
<span id="cb33-79"><a href="#cb33-79" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-80"><a href="#cb33-80" aria-hidden="true" tabindex="-1"></a><span class="fu">## General rules for using an API</span></span>
<span id="cb33-81"><a href="#cb33-81" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-82"><a href="#cb33-82" aria-hidden="true" tabindex="-1"></a>To use an API to extract data, you will need to follow these steps:</span>
<span id="cb33-83"><a href="#cb33-83" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-84"><a href="#cb33-84" aria-hidden="true" tabindex="-1"></a><span class="ss">1. </span>Find an API that provides the data you are interested in. This may involve doing some research online to find available APIs.</span>
<span id="cb33-85"><a href="#cb33-85" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-86"><a href="#cb33-86" aria-hidden="true" tabindex="-1"></a><span class="ss">2. </span>Familiarize yourself with the API's documentation to understand how to make requests and what data is available.</span>
<span id="cb33-87"><a href="#cb33-87" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-88"><a href="#cb33-88" aria-hidden="true" tabindex="-1"></a><span class="ss">3. </span>Use a programming language to write a script that sends a request to the API and receives the data. Depending on the API, you may need to include authentication parameters in the request to specify the data you want to receive.</span>
<span id="cb33-89"><a href="#cb33-89" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-90"><a href="#cb33-90" aria-hidden="true" tabindex="-1"></a><span class="ss">4. </span>Parse the data you receive from the API to extract the information you are interested in. The format of the data will depend on the API you are using, and may be in JSON, XML, or another format.</span>
<span id="cb33-91"><a href="#cb33-91" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-92"><a href="#cb33-92" aria-hidden="true" tabindex="-1"></a><span class="ss">5. </span>Process the extracted data in your script, or save it to a file or database for later analysis.</span>
<span id="cb33-93"><a href="#cb33-93" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-94"><a href="#cb33-94" aria-hidden="true" tabindex="-1"></a><span class="fu">## How to fetch API data using a programming language</span></span>
<span id="cb33-95"><a href="#cb33-95" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-96"><a href="#cb33-96" aria-hidden="true" tabindex="-1"></a><span class="fu">### with R {#sec-api-r}</span></span>
<span id="cb33-97"><a href="#cb33-97" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-98"><a href="#cb33-98" aria-hidden="true" tabindex="-1"></a>There are several packages available in R for consuming APIs. Some of the most commonly used packages are:</span>
<span id="cb33-99"><a href="#cb33-99" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-100"><a href="#cb33-100" aria-hidden="true" tabindex="-1"></a><span class="ss">1. </span>**httr**: This package provides convenient functions for making HTTP requests and processing the responses.</span>
<span id="cb33-101"><a href="#cb33-101" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-102"><a href="#cb33-102" aria-hidden="true" tabindex="-1"></a><span class="ss">2. </span>**jsonlite**: This package is used for parsing JSON data, which is a common format for API responses.</span>
<span id="cb33-103"><a href="#cb33-103" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-104"><a href="#cb33-104" aria-hidden="true" tabindex="-1"></a><span class="ss">3. </span>**RCurl**: This package is a wrapper around the libcurl library, which is a powerful and versatile HTTP client library.</span>
<span id="cb33-105"><a href="#cb33-105" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-106"><a href="#cb33-106" aria-hidden="true" tabindex="-1"></a>To get data from an API in R, you need to follow these steps:</span>
<span id="cb33-107"><a href="#cb33-107" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-108"><a href="#cb33-108" aria-hidden="true" tabindex="-1"></a><span class="ss">1. </span>Install the required packages by running the following command in the R console:</span>
<span id="cb33-109"><a href="#cb33-109" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-112"><a href="#cb33-112" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-113"><a href="#cb33-113" aria-hidden="true" tabindex="-1"></a><span class="co">#| code-summary: "installation of packages"</span></span>
<span id="cb33-114"><a href="#cb33-114" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-115"><a href="#cb33-115" aria-hidden="true" tabindex="-1"></a><span class="fu">install.packages</span>(<span class="fu">c</span>(<span class="st">"httr"</span>, <span class="st">"jsonlite"</span>, <span class="st">"RCurl"</span>)) </span>
<span id="cb33-116"><a href="#cb33-116" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-117"><a href="#cb33-117" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-118"><a href="#cb33-118" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-119"><a href="#cb33-119" aria-hidden="true" tabindex="-1"></a><span class="ss">2. </span>Load the packages by running the following command:</span>
<span id="cb33-120"><a href="#cb33-120" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-123"><a href="#cb33-123" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-124"><a href="#cb33-124" aria-hidden="true" tabindex="-1"></a><span class="co">#| code-summary: "Loading of packages"</span></span>
<span id="cb33-125"><a href="#cb33-125" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-126"><a href="#cb33-126" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(httr) </span>
<span id="cb33-127"><a href="#cb33-127" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-128"><a href="#cb33-128" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(jsonlite) </span>
<span id="cb33-129"><a href="#cb33-129" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-130"><a href="#cb33-130" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(RCurl) </span>
<span id="cb33-131"><a href="#cb33-131" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-132"><a href="#cb33-132" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-133"><a href="#cb33-133" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-134"><a href="#cb33-134" aria-hidden="true" tabindex="-1"></a><span class="ss">3. </span>Make an API request by using the GET function from the httr package. The API endpoint should be passed as an argument to this function.</span>
<span id="cb33-135"><a href="#cb33-135" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-138"><a href="#cb33-138" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-139"><a href="#cb33-139" aria-hidden="true" tabindex="-1"></a>response <span class="ot"><-</span> <span class="fu">GET</span>(<span class="st">"https://api.example.com/endpoint"</span>) </span>
<span id="cb33-140"><a href="#cb33-140" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-141"><a href="#cb33-141" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-142"><a href="#cb33-142" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-143"><a href="#cb33-143" aria-hidden="true" tabindex="-1"></a><span class="ss">4. </span>Check the status code of the response to see if the request was successful. A status code of <span class="in">`200`</span> indicates a successful request.</span>
<span id="cb33-144"><a href="#cb33-144" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-147"><a href="#cb33-147" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-148"><a href="#cb33-148" aria-hidden="true" tabindex="-1"></a><span class="co">#| code-summary: "Request status"</span></span>
<span id="cb33-149"><a href="#cb33-149" aria-hidden="true" tabindex="-1"></a><span class="co">#| </span></span>
<span id="cb33-150"><a href="#cb33-150" aria-hidden="true" tabindex="-1"></a>status_code <span class="ot"><-</span> <span class="fu">status_code</span>(response) </span>
<span id="cb33-151"><a href="#cb33-151" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-152"><a href="#cb33-152" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-153"><a href="#cb33-153" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-154"><a href="#cb33-154" aria-hidden="true" tabindex="-1"></a><span class="ss">5. </span>Extract the data from the response. If the API returns data in JSON format, you can use the <span class="in">`fromJSON`</span> function from the <span class="in">`jsonlite`</span> package to parse the data. Store the data in a variable for later use.</span>
<span id="cb33-155"><a href="#cb33-155" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-158"><a href="#cb33-158" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-159"><a href="#cb33-159" aria-hidden="true" tabindex="-1"></a><span class="co">#| code-summary: "Resulting dataframe"</span></span>
<span id="cb33-160"><a href="#cb33-160" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-161"><a href="#cb33-161" aria-hidden="true" tabindex="-1"></a>api_data <span class="ot"><-</span> <span class="fu">fromJSON</span>(<span class="fu">content</span>(response, <span class="at">as =</span> <span class="st">"text"</span>)) </span>
<span id="cb33-162"><a href="#cb33-162" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-163"><a href="#cb33-163" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-164"><a href="#cb33-164" aria-hidden="true" tabindex="-1"></a>These are the basic steps to get data from an API in R. Depending on the API, you may need to pass additional parameters or authentication information in your request. For example,</span>
<span id="cb33-165"><a href="#cb33-165" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-168"><a href="#cb33-168" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-169"><a href="#cb33-169" aria-hidden="true" tabindex="-1"></a><span class="co">#| code-summary: "API with access key"</span></span>
<span id="cb33-170"><a href="#cb33-170" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-171"><a href="#cb33-171" aria-hidden="true" tabindex="-1"></a>response <span class="ot"><-</span> <span class="fu">GET</span>(<span class="st">"https://api.example.com/endpoint"</span>,</span>
<span id="cb33-172"><a href="#cb33-172" aria-hidden="true" tabindex="-1"></a> <span class="fu">authenticate</span>(</span>
<span id="cb33-173"><a href="#cb33-173" aria-hidden="true" tabindex="-1"></a> <span class="at">user =</span> <span class="st">"API_KEY_HERE"</span>,</span>
<span id="cb33-174"><a href="#cb33-174" aria-hidden="true" tabindex="-1"></a> <span class="at">password =</span> <span class="st">"API_PASSWORD_HERE"</span>,</span>
<span id="cb33-175"><a href="#cb33-175" aria-hidden="true" tabindex="-1"></a> <span class="at">type =</span> <span class="st">"basic"</span>)) </span>
<span id="cb33-176"><a href="#cb33-176" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-177"><a href="#cb33-177" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-178"><a href="#cb33-178" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-179"><a href="#cb33-179" aria-hidden="true" tabindex="-1"></a><span class="fu">### with Python {#sec-api-py}</span></span>
<span id="cb33-180"><a href="#cb33-180" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-181"><a href="#cb33-181" aria-hidden="true" tabindex="-1"></a>To use an API in Python, you can use a library such as <span class="in">`requests`</span> or <span class="in">`urllib`</span> to send HTTP requests to the API and receive responses. Here's an example of how to use an API in Python using the requests library:</span>
<span id="cb33-182"><a href="#cb33-182" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-185"><a href="#cb33-185" aria-hidden="true" tabindex="-1"></a><span class="in">```{python}</span></span>
<span id="cb33-186"><a href="#cb33-186" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> requests</span>
<span id="cb33-187"><a href="#cb33-187" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-188"><a href="#cb33-188" aria-hidden="true" tabindex="-1"></a><span class="co"># Define the API endpoint URL and parameters</span></span>
<span id="cb33-189"><a href="#cb33-189" aria-hidden="true" tabindex="-1"></a>endpoint <span class="op">=</span> <span class="st">'https://api.example.com/data'</span></span>
<span id="cb33-190"><a href="#cb33-190" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-191"><a href="#cb33-191" aria-hidden="true" tabindex="-1"></a>params <span class="op">=</span> {<span class="st">'param1'</span>: <span class="st">'value1'</span>, <span class="st">'param2'</span>: <span class="st">'value2'</span>}</span>
<span id="cb33-192"><a href="#cb33-192" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-193"><a href="#cb33-193" aria-hidden="true" tabindex="-1"></a><span class="co"># Send a GET request to the API endpoint</span></span>
<span id="cb33-194"><a href="#cb33-194" aria-hidden="true" tabindex="-1"></a>response <span class="op">=</span> requests.get(endpoint, params <span class="op">=</span> params)</span>
<span id="cb33-195"><a href="#cb33-195" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-196"><a href="#cb33-196" aria-hidden="true" tabindex="-1"></a><span class="co"># Check if the request was successful</span></span>
<span id="cb33-197"><a href="#cb33-197" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> response.status_code <span class="op">==</span> <span class="dv">200</span>:</span>
<span id="cb33-198"><a href="#cb33-198" aria-hidden="true" tabindex="-1"></a> <span class="co"># Parse the response JSON data</span></span>
<span id="cb33-199"><a href="#cb33-199" aria-hidden="true" tabindex="-1"></a> data <span class="op">=</span> response.json()</span>
<span id="cb33-200"><a href="#cb33-200" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-201"><a href="#cb33-201" aria-hidden="true" tabindex="-1"></a> <span class="co"># Process the data, for example by printing it to the console</span></span>
<span id="cb33-202"><a href="#cb33-202" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(data)</span>
<span id="cb33-203"><a href="#cb33-203" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span>:</span>
<span id="cb33-204"><a href="#cb33-204" aria-hidden="true" tabindex="-1"></a> <span class="bu">print</span>(<span class="ss">f'Error: </span><span class="sc">{</span>response<span class="sc">.</span>status_code<span class="sc">}</span><span class="ss">'</span>)</span>
<span id="cb33-205"><a href="#cb33-205" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-206"><a href="#cb33-206" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-207"><a href="#cb33-207" aria-hidden="true" tabindex="-1"></a>In this example, we're using the <span class="in">`requests`</span> library to send a GET request to an API endpoint at <span class="in">`https://api.example.com/data`</span>, passing two parameters (<span class="in">`param1`</span> and <span class="in">`param2`</span>) in the request. The <span class="in">`requests.get()`</span> method returns a <span class="in">`Response`</span> object, which we can use to check the response status code and parse the response data.</span>
<span id="cb33-208"><a href="#cb33-208" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-209"><a href="#cb33-209" aria-hidden="true" tabindex="-1"></a>If the status code is <span class="in">`200`</span>, we can assume the request was successful, and we can parse the response data using the <span class="in">`response.json()`</span> method, which converts the JSON-formatted response to a Python object. We can then process the data as needed, for example by printing it to the console.</span>
<span id="cb33-210"><a href="#cb33-210" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-211"><a href="#cb33-211" aria-hidden="true" tabindex="-1"></a>Of course, the exact API endpoint and parameters will depend on the specific API that you are using, and you'll need to consult the API documentation to learn how to construct your request correctly. But this example should give you a sense of the general process involved in using an API in Python.</span>
<span id="cb33-212"><a href="#cb33-212" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-213"><a href="#cb33-213" aria-hidden="true" tabindex="-1"></a><span class="fu">## Practical example in R and Python</span></span>
<span id="cb33-214"><a href="#cb33-214" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-215"><a href="#cb33-215" aria-hidden="true" tabindex="-1"></a>We will use R and Python to fetch the API data without and with the key.</span>
<span id="cb33-216"><a href="#cb33-216" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-217"><a href="#cb33-217" aria-hidden="true" tabindex="-1"></a><span class="fu">### Without the key</span></span>
<span id="cb33-218"><a href="#cb33-218" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-219"><a href="#cb33-219" aria-hidden="true" tabindex="-1"></a>In this example, we will use an API from a site called <span class="co">[</span><span class="al"></span><span class="ot">{height="30"}</span><span class="co">](https://www.givefood.org.uk/api/2/docs/)</span> that uses an API without an API key. In this case, we will be using a <span class="in">`GET`</span> request to fetch the API data at the **food banks** using this link: <span class="ot"><https://www.givefood.org.uk/api/2/foodbanks></span>. Please follow the steps in @sec-api-r for R and @sec-api-py for Python.</span>
<span id="cb33-220"><a href="#cb33-220" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-221"><a href="#cb33-221" aria-hidden="true" tabindex="-1"></a>::: {.panel-tabset group="language"}</span>
<span id="cb33-222"><a href="#cb33-222" aria-hidden="true" tabindex="-1"></a><span class="fu">## R</span></span>
<span id="cb33-223"><a href="#cb33-223" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-226"><a href="#cb33-226" aria-hidden="true" tabindex="-1"></a><span class="in">```{r}</span></span>
<span id="cb33-227"><a href="#cb33-227" aria-hidden="true" tabindex="-1"></a><span class="co">#| eval: true</span></span>
<span id="cb33-228"><a href="#cb33-228" aria-hidden="true" tabindex="-1"></a><span class="co">#| </span></span>
<span id="cb33-229"><a href="#cb33-229" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-230"><a href="#cb33-230" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(httr)</span>
<span id="cb33-231"><a href="#cb33-231" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-232"><a href="#cb33-232" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(jsonlite)</span>
<span id="cb33-233"><a href="#cb33-233" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-234"><a href="#cb33-234" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span>
<span id="cb33-235"><a href="#cb33-235" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-236"><a href="#cb33-236" aria-hidden="true" tabindex="-1"></a>response <span class="ot"><-</span> <span class="fu">GET</span>(<span class="st">"https://www.givefood.org.uk/api/2/foodbanks"</span>)</span>
<span id="cb33-237"><a href="#cb33-237" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-238"><a href="#cb33-238" aria-hidden="true" tabindex="-1"></a><span class="fu">status_code</span>(response)</span>
<span id="cb33-239"><a href="#cb33-239" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-240"><a href="#cb33-240" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="ot"><-</span> <span class="fu">fromJSON</span>(<span class="fu">content</span>(response, <span class="at">as =</span> <span class="st">"text"</span>), <span class="at">flatten =</span> <span class="cn">TRUE</span>)</span>
<span id="cb33-241"><a href="#cb33-241" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-242"><a href="#cb33-242" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="sc">%>%</span> </span>
<span id="cb33-243"><a href="#cb33-243" aria-hidden="true" tabindex="-1"></a> <span class="fu">dim</span>()</span>
<span id="cb33-244"><a href="#cb33-244" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-245"><a href="#cb33-245" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="sc">%>%</span> </span>
<span id="cb33-246"><a href="#cb33-246" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span>()</span>
<span id="cb33-247"><a href="#cb33-247" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-248"><a href="#cb33-248" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-249"><a href="#cb33-249" aria-hidden="true" tabindex="-1"></a><span class="fu">## Python</span></span>
<span id="cb33-250"><a href="#cb33-250" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-253"><a href="#cb33-253" aria-hidden="true" tabindex="-1"></a><span class="in">```{python}</span></span>
<span id="cb33-254"><a href="#cb33-254" aria-hidden="true" tabindex="-1"></a><span class="co">#| eval: true</span></span>
<span id="cb33-255"><a href="#cb33-255" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-256"><a href="#cb33-256" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> requests</span>
<span id="cb33-257"><a href="#cb33-257" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-258"><a href="#cb33-258" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pandas <span class="im">as</span> pd</span>
<span id="cb33-259"><a href="#cb33-259" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-260"><a href="#cb33-260" aria-hidden="true" tabindex="-1"></a>response <span class="op">=</span> requests.get(<span class="st">"https://www.givefood.org.uk/api/2/foodbanks"</span>)</span>
<span id="cb33-261"><a href="#cb33-261" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-262"><a href="#cb33-262" aria-hidden="true" tabindex="-1"></a><span class="co"># Check if the request was successful</span></span>
<span id="cb33-263"><a href="#cb33-263" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-264"><a href="#cb33-264" aria-hidden="true" tabindex="-1"></a><span class="bu">print</span>(response.status_code)</span>
<span id="cb33-265"><a href="#cb33-265" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-266"><a href="#cb33-266" aria-hidden="true" tabindex="-1"></a><span class="co"># Parse the response JSON data</span></span>
<span id="cb33-267"><a href="#cb33-267" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-268"><a href="#cb33-268" aria-hidden="true" tabindex="-1"></a>food_json <span class="op">=</span> response.json()</span>
<span id="cb33-269"><a href="#cb33-269" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-270"><a href="#cb33-270" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert to a pandas dataframe</span></span>
<span id="cb33-271"><a href="#cb33-271" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-272"><a href="#cb33-272" aria-hidden="true" tabindex="-1"></a>food_dataframe <span class="op">=</span> pd.json_normalize(food_json)</span>
<span id="cb33-273"><a href="#cb33-273" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-274"><a href="#cb33-274" aria-hidden="true" tabindex="-1"></a>food_dataframe.shape</span>
<span id="cb33-275"><a href="#cb33-275" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb33-276"><a href="#cb33-276" aria-hidden="true" tabindex="-1"></a>food_dataframe.head()</span>
<span id="cb33-277"><a href="#cb33-277" aria-hidden="true" tabindex="-1"></a><span class="in">```</span></span>
<span id="cb33-278"><a href="#cb33-278" aria-hidden="true" tabindex="-1"></a></span>