{"id":9157,"date":"2020-07-30T07:16:07","date_gmt":"2020-07-30T07:16:07","guid":{"rendered":"https:\/\/www.experfy.com\/blog\/?p=9157"},"modified":"2023-11-24T13:31:39","modified_gmt":"2023-11-24T13:31:39","slug":"a-laymans-guide-for-data-scientists-to-create-apis-in-minutes","status":"publish","type":"post","link":"https:\/\/www.experfy.com\/blog\/software-ux-ui\/a-laymans-guide-for-data-scientists-to-create-apis-in-minutes\/","title":{"rendered":"A Layman\u2019s Guide for Data Scientists to create APIs in minutes"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"9157\" class=\"elementor elementor-9157\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"has_eae_slider elementor-section elementor-top-section elementor-element elementor-element-57ffbe31 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"57ffbe31\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"has_eae_slider elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-2886e36b\" data-id=\"2886e36b\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-fe81ec2 elementor-widget elementor-widget-text-editor\" data-id=\"fe81ec2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\n<p>Have you ever been in a situation where you want to provide your model predictions to a frontend developer without them having access to model related code? Or has a developer ever asked you to create an API that they can use? I have faced this a lot.<\/p>\n\n\n\n<p>As Data Science and Web developers try to collaborate, API\u2019s become an essential piece of the puzzle to make codes as well as skills more modular. In fact, in the same way, that a data scientist can\u2019t be expected to know much about Javascript or nodeJS, a frontend developer should be able to get by without knowing any Data Science Language. And APIs do play a considerable role in this abstraction.<\/p>\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8367513 elementor-widget elementor-widget-text-editor\" data-id=\"8367513\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>But, APIs are confusing. I myself have been confused a lot while creating and sharing them with my development teams who talk in their API terminology like GET request, PUT request, endpoint, Payloads, etc.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong><em>This post will be about simplifying and understanding how APIs work, explaining some of the above terms, and creating an API using the excellent API building framework called FastAPI, which makes creating APIs a breeze.<\/em><\/strong><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator --><hr class=\"wp-block-separator\" \/><!-- \/wp:separator -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-59ec529 elementor-widget elementor-widget-heading\" data-id=\"59ec529\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"cdd9\">What is an API?<\/h1><\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4b22f92 elementor-widget elementor-widget-text-editor\" data-id=\"4b22f92\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>Before we go any further, we need to understand what an API is. According to Wikipedia:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\">\n<p>An\u00a0<strong>application programming interface<\/strong>\u00a0(<strong>API<\/strong>) is a\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Interface_(computing)\" target=\"_blank\" rel=\"noreferrer noopener\">computing interface<\/a>\u00a0which defines interactions between multiple software intermediaries. It defines the kinds of calls or requests that can be made, how to make them, the data formats that should be used, the conventions to follow, etc.<\/p>\n<\/blockquote>\n<!-- \/wp:quote -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6c13d8f elementor-widget elementor-widget-text-editor\" data-id=\"6c13d8f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><em>The way I like to understand an API is that it\u2019s an \u201conline function,\u201d a function that I can call online.<\/em><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>For example:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>I can have a movie API, which returns me names of drama movies when I pass the \u201canimation\u201d genre as input.<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-dca773f elementor-widget elementor-widget-image\" data-id=\"dca773f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/591\/1*r6cfZ3DWKHCe_6aEgFGcsg.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-73169f6 elementor-widget elementor-widget-text-editor\" data-id=\"73169f6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><strong><em>The advantage of using such a sort of mechanism is that the API user doesn\u2019t get access to the whole dataset or source code and yet they can get all the information they need.\u00a0<\/em><\/strong><em>This is how many services on the internet like\u00a0<\/em><a href=\"https:\/\/aws.amazon.com\/rekognition\/\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Amazon Rekognition<\/em><\/a><em>, which is an image and video API, or\u00a0<\/em><a href=\"https:\/\/cloud.google.com\/natural-language\/pricing\" target=\"_blank\" rel=\"noreferrer noopener\"><em>Google Natural Language API<\/em><\/a><em>, which is an NLP API works. They provide us access to some great functions without letting us have the source code, which is often valuable and kept hidden. For example, I can send an image to Amazon Rekognition API, and it can provide me with Face detection and Analysis.<\/em><\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c3eed31 elementor-widget elementor-widget-image\" data-id=\"c3eed31\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1224\/0*h1tXEQkpWcI3ZKMu.jpg\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9e092e7 elementor-widget elementor-widget-text-editor\" data-id=\"9e092e7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>For example, here is a free API floated by Open Movie DB, which lets us search for movies using parameters:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"><a href=\"http:\/\/www.omdbapi.com\/?i=tt3896198&amp;apikey=9ea43e94\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/www.omdbapi.com\/?i=tt3896198&amp;apikey=9ea43e94<\/a><\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-625fc46 elementor-widget elementor-widget-text-editor\" data-id=\"625fc46\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Here I provided the IMDB id for the movie Guardians of the Galaxy 2, using the\u00a0<code>i<\/code>\u00a0parameter for the API. If you open this link in your browser, you will see the whole information of the movie as per the Open Movie Database<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9448fad elementor-widget elementor-widget-image\" data-id=\"9448fad\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1262\/1*pvIcnQIJFlEGasGhUqwn3Q.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f58f5d1 elementor-widget elementor-widget-text-editor\" data-id=\"f58f5d1\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>But before we go any further, let\u2019s understand some terms:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:list -->\n<ul>\n<li><strong>Endpoint:<\/strong>\u00a0In the above API call, the endpoint is :\u00a0<code>http:\/\/www.omdbapi.com\/<\/code>\u00a0. Simply this is the location of where the function code is running.<\/li>\n<li><strong>API Access Key<\/strong>: Most of the public APIs will have some access key, which you can request. For OMDB API, I had to register and get the API key which is\u00a0<code><a href=\"http:\/\/www.omdbapi.com\/?i=tt3896198&amp;apikey=9ea43e94\" target=\"_blank\" rel=\"noreferrer noopener\">9ea43e94<\/a><\/code>.<\/li>\n<li><code><strong>?<\/strong><\/code><strong>\u00a0Operator:\u00a0<\/strong>This operator is used to specify the parameters we want to send to the API or our \u201conline function.\u201d Here we give two params to our API i.e., IMDB movie ID and API Access Key using the\u00a0<code>?<\/code>\u00a0operator. Since there are multiple inputs, we use\u00a0<code>&amp;<\/code>\u00a0operator also.<\/li>\n<\/ul>\n<!-- \/wp:list -->\n\n<!-- wp:separator --><hr class=\"wp-block-separator\" \/><!-- \/wp:separator -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-edd1cf6 elementor-widget elementor-widget-heading\" data-id=\"edd1cf6\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"2151\">Why FastAPI?<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bbd86d4 elementor-widget elementor-widget-text-editor\" data-id=\"bbd86d4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p><em>\u201cIf you\u2019re looking to learn one modern framework for building REST APIs, check out FastAPI [\u2026] It\u2019s fast, easy to use and easy to learn [\u2026]<\/em>\u201d \u2014\u00a0<a href=\"https:\/\/spacy.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">spaCy<\/a>\u00a0creators<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>While Python has many frameworks to build APIs, the most common being Flask and Tornado, FastAPI is much better than available alternatives in its ease of usage as it seems much more pythonic in comparison with Flask.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Also, FastAPI is fast. As the Github docs say,\u00a0<strong><em>\u201cVery high performance, on par with NodeJS and Go.\u201d<\/em><\/strong><em>\u00a0We can also check the latency\u00a0<\/em><a href=\"https:\/\/www.techempower.com\/benchmarks\/#section=test&amp;runid=7464e520-0dc2-473d-bd34-dbdfd7e85911&amp;hw=ph&amp;test=query&amp;l=z8kflr-v&amp;a=2\" target=\"_blank\" rel=\"noreferrer noopener\"><em>benchmarks<\/em><\/a><em>\u00a0for ourselves.<\/em><\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-10c4b48 elementor-widget elementor-widget-image\" data-id=\"10c4b48\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1759\/1*d2kH0XrlFgkXCF5EYvECvQ.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d3d9c43 elementor-widget elementor-widget-text-editor\" data-id=\"d3d9c43\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>That is around a speedup by a factor of 2 when compared to Flask and that too without a lot of code change. This means a huge deal when it comes to building an API that can serve millions of customers as it can reduce production efforts and also use less expensive hardware to serve.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>So enough of comparison and talk, let\u2019s try to use FastAPI to create our API.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator --><hr class=\"wp-block-separator\" \/><!-- \/wp:separator -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f5e144b elementor-widget elementor-widget-heading\" data-id=\"f5e144b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h1 class=\"elementor-heading-title elementor-size-default\"><h1 id=\"b9c0\">How to write an API with FastAPI?<\/h1><\/h1>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-becfd53 elementor-widget elementor-widget-text-editor\" data-id=\"becfd53\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>One of the most common use cases for Data Science is how to create an API for getting a model\u2019s prediction? Let us assume that we have a Titanic Survival model in place that predicts if a person will survive or not. And, it needs a person\u2019s\u00a0<code>age<\/code>\u00a0and\u00a0<code>sex<\/code>\u00a0as input params to predict. We will create this API using FastAPI in two ways: GET and PUT. Don\u2019t worry; I will explain each as we go.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>What is GET? \u2014\u00a0<\/strong>In a GET request, we usually try to retrieve data using query parameters that are embedded in the query string itself. For example, in the OMDB API, we use the GET request to specify the movie id and access key as part of the query itself.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><strong>What is PUT? \u2014\u00a0<\/strong>An alternative to the GET request is the PUT request, where we send parameters using a payload, as we will see in the second method. The payload is not part of the query string, and thus PUT is more secure. It will become more clear when you see the second part.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>But before we go any further, we need to install FastAPI and\u00a0<code>uvicorn<\/code>\u00a0ASGI server with:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">pip install fastapi<br \/>pip install uvicorn<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8ff01d8 elementor-widget elementor-widget-heading\" data-id=\"8ff01d8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\"><h2 id=\"9207\">1. The GET Way:<\/h2><\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f685fd8 elementor-widget elementor-widget-text-editor\" data-id=\"f685fd8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>A simple FastAPI method to writing a GET API for our titanic model use case is as follows:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">from fastapi import FastAPIapp = FastAPI()@app.get(\"\/predict\")<br \/>def predict_complex_model(age: int,sex:str):<br \/>    # Assume a big and complex model here. For this test I am using a simple rule based model<br \/>    if age&lt;10 or sex=='F':<br \/>        return {'survived':1}<br \/>    else:<br \/>        return {'survived':0}<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>Save the above code in a file named\u00a0<code>fastapiapp.py<\/code>\u00a0and then you can run it using the below command on terminal.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">$ uvicorn fastapiapp:app --reload<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>The above means that your API is now running on your server, and the\u00a0<code>--reload<\/code>\u00a0flag indicates that the API gets updated automatically when you change the\u00a0<code>fastapiapp.py<\/code>\u00a0file. This is very helpful while developing and testing, but you should remove this\u00a0<code>--reload<\/code>\u00a0flag when you put the API in production. Now you can visit the below path in your browser, and you will get the prediction results:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"><a href=\"http:\/\/127.0.0.1:8000\/predict?age=10&amp;sex=M\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/127.0.0.1:8000\/predict?age=10&amp;sex=M<\/a><\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-caa5c76 elementor-widget elementor-widget-image\" data-id=\"caa5c76\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/535\/1*bEVyR5O4L7i1Mva8hdY7IQ.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a659b2d elementor-widget elementor-widget-text-editor\" data-id=\"a659b2d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>What happens is as you hit the command in your browser, it calls the\u00a0<code><a href=\"http:\/\/127.0.0.1:8000\/predict?age=10&amp;sex=M\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/127.0.0.1:8000\/<\/a>predict<\/code>\u00a0endpoint which in turn calls the associated method\u00a0<code>predict_complex_model\u00a0<\/code>with the with params\u00a0<code>age=10 and sex='M'<\/code><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>So, it allows us to use our function from a browser, but that\u2019s still not very helpful. Your developer friend needs to use your predict function to show output on a frontend website. How can you provide him with access to this function?<\/p>\n<!-- \/wp:paragraph -->\n\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3d94664 elementor-widget elementor-widget-text-editor\" data-id=\"3d94664\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>It is pretty simple. If your developer friend also uses Python, for example, he can use the\u00a0<code>requests<\/code>\u00a0module like below:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">import requestsage = 15<br \/>sex = \"F\"response = requests.get(f\"<a href=\"http:\/\/127.0.0.1:8000\/predict?age=10&amp;sex=M\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/127.0.0.1:8000\/predict?age={age}&amp;sex={<\/a>sex}\")<br \/>output = response.json()<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-fb7cec8 elementor-widget elementor-widget-text-editor\" data-id=\"fb7cec8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>So we can get the output from the running API(on the server) into our Python Program. A Javascript user would use Javascript Request Library, and a nodeJS developer will use something similar to do this in nodeJS. We will just need to provide them with the endpoint and parameters required.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>To test your API, you could also go to the:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"><a href=\"http:\/\/127.0.0.1:8000\/docs\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/127.0.0.1:8000\/docs<\/a><\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>Where you will find a GUI way to test your API.<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-929ff94 elementor-widget elementor-widget-image\" data-id=\"929ff94\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1575\/1*fk-KkBlKE8kYG8VE0y_J6w.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-7d05dab elementor-widget elementor-widget-text-editor\" data-id=\"7d05dab\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>But as we said earlier,\u00a0<strong>THIS IS NOT SECURE<\/strong>\u00a0as GET parameters are passed via URL. This means that parameters get stored in server logs and browser history. This is not intended. Further, this toy example just had two input parameters, so we were able to do it this way, think of a case where we need to provide many parameters to our predict function.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>In such a case or I dare say in most of the cases, we use the PUT API.<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-759ff4e elementor-widget elementor-widget-heading\" data-id=\"759ff4e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"heading.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t<h2 class=\"elementor-heading-title elementor-size-default\"><h2 id=\"a5aa\">2. The PUT Way<\/h2><\/h2>\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4204109 elementor-widget elementor-widget-text-editor\" data-id=\"4204109\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Using the PUT API, we can call any function by providing a payload to the function. A payload is nothing but a JSON dictionary of input parameters that doesn\u2019t get appended to the query string and is thus much more secure than GET.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Here is the minimal example where we do that same thing as before using PUT. We just change the content of\u00a0<code>fastapiapp.py<\/code>\u00a0to:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">from fastapi import FastAPI<br \/>from pydantic import BaseModelclass Input(BaseModel):<br \/>    age : int<br \/>    sex : strapp = FastAPI()<a href=\"http:\/\/twitter.com\/app\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"broken_link\">@app<\/a>.put(\"\/predict\")<br \/>def predict_complex_model(d:Input):<br \/>    if d.age&lt;10 or d.sex=='F':<br \/>        return {'survived':1}<br \/>    else:<br \/>        return {'survived':0}<\/pre>\n<!-- \/wp:preformatted -->\n\n<!-- wp:paragraph -->\n<p>note that we use\u00a0<code>app.put<\/code>\u00a0here in place of\u00a0<code>app.get<\/code>\u00a0previously. We also needed to provide a new class\u00a0<code>Input<\/code>\u00a0, which uses a library called\u00a0<code>pydantic<\/code>\u00a0to validate the input data types that we will get from the API end-user while previously in GET, we validated the inputs using the function parameter list. Also, this time you won\u2019t be able to see your content using a URL on the web. For example, using the browser to point to the endpoint location gives:<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9299df4 elementor-widget elementor-widget-image\" data-id=\"9299df4\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/377\/1*K81f-8fTyUpgbHql2d3LLw.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8340556 elementor-widget elementor-widget-text-editor\" data-id=\"8340556\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>So, we can check using the programmatic way using requests in Python again:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\">import requests,json<br \/>payload = json.dumps({<br \/>  \"age\": 10,<br \/>  \"sex\": \"F\"<br \/>})<br \/>response = requests.put(\"<a href=\"http:\/\/127.0.0.1:8000\/predict\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/127.0.0.1:8000\/predict<\/a>\",data = payload)<br \/>response.json()<\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6706501 elementor-widget elementor-widget-image\" data-id=\"6706501\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/896\/1*pWVrZdw8iYCiScqoT1_7pg.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-bf6c8b7 elementor-widget elementor-widget-text-editor\" data-id=\"bf6c8b7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Notice that we use\u00a0<code>requests.put<\/code>\u00a0here and we provide the payload using the data param in the\u00a0<code>requests.put<\/code>\u00a0function and we also make use of\u00a0<code>json<\/code>\u00a0library to convert our payload to JSON from a dict object.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>We could also have used the GUI way as before using:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:preformatted -->\n<pre class=\"wp-block-preformatted\"><a href=\"http:\/\/127.0.0.1:8000\/docs\" target=\"_blank\" rel=\"noreferrer noopener\">http:\/\/127.0.0.1:8000\/docs<\/a><\/pre>\n<!-- \/wp:preformatted -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-654c55b elementor-widget elementor-widget-image\" data-id=\"654c55b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" src=\"https:\/\/miro.medium.com\/max\/1314\/1*aZkWz1DDg_qwa8SJhCF1nw.png\" alt=\"\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b18f647 elementor-widget elementor-widget-text-editor\" data-id=\"b18f647\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<!-- wp:paragraph -->\n<p>And, we are done with creating our API. It was simple for a change.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>FastAPI makes the API creation, which used to be one of the dreaded parts of the Data Science process, much more intuitive, easy, and Fast.<\/p>\n<!-- \/wp:paragraph -->\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>This post will be about simplifying and understanding how APIs work, explaining some of the terms, and creating an API using the excellent API building framework called FastAPI, which makes creating APIs a breeze. <\/p>\n","protected":false},"author":653,"featured_media":9164,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[200],"tags":[505,394,506],"ppma_author":[3409],"class_list":["post-9157","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-ux-ui","tag-api","tag-data-scientist","tag-fastapi"],"authors":[{"term_id":3409,"user_id":653,"is_guest":0,"slug":"rahul-agarwal","display_name":"Rahul Agarwal","avatar_url":"https:\/\/www.experfy.com\/blog\/wp-content\/uploads\/2020\/04\/medium_cc5785b8-8195-44e6-a0de-2e33be05d7cb-150x150.png","user_url":"http:\/\/bit.ly\/384SBYb","last_name":"Agarwal","first_name":"Rahul","job_title":"","description":"Rahul Agarwal is a Data Scientist at Walmart Labs."}],"_links":{"self":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/9157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/users\/653"}],"replies":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/comments?post=9157"}],"version-history":[{"count":5,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/9157\/revisions"}],"predecessor-version":[{"id":34341,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/9157\/revisions\/34341"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media\/9164"}],"wp:attachment":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media?parent=9157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/categories?post=9157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/tags?post=9157"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=9157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}