{"id":49,"date":"2023-05-13T05:27:05","date_gmt":"2023-05-13T05:27:05","guid":{"rendered":"https:\/\/feellikelearning.com\/en\/?p=49"},"modified":"2023-05-13T05:27:05","modified_gmt":"2023-05-13T05:27:05","slug":"python-fastapi-simple-example","status":"publish","type":"post","link":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/","title":{"rendered":"Python fastapi simple example"},"content":{"rendered":"\n<p>Here&#8217;s a simple example of a Python FastAPI application:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from fastapi import FastAPI\n\napp = FastAPI()\n\n@app.get(\"\/\")\ndef read_root():\n    return {\"Hello\": \"World\"}\n\n@app.get(\"\/items\/{item_id}\")\ndef read_item(item_id: int, q: str = None):\n    return {\"item_id\": item_id, \"q\": q}\n\nif __name__ == \"__main__\":\n    import uvicorn\n    uvicorn.run(app, host=\"0.0.0.0\", port=8000)\n<\/code><\/pre>\n\n\n\n<p>In this example, we create a FastAPI instance called <code>app<\/code>. We define two routes: the root route (&#8220;\/&#8221;) and an item route (&#8220;\/items\/{item_id}&#8221;). The root route returns a simple JSON response of {&#8220;Hello&#8221;: &#8220;World&#8221;}, while the item route takes an <code>item_id<\/code> as a path parameter and an optional query parameter <code>q<\/code>. It returns a JSON response containing the provided <code>item_id<\/code> and <code>q<\/code> values.<\/p>\n\n\n\n<p>To run the application, you&#8217;ll need to install FastAPI and Uvicorn. You can do so using pip:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install fastapi uvicorn\n<\/code><\/pre>\n\n\n\n<p>Save the code to a file, for example, <code>main.py<\/code>. Then, you can start the server by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uvicorn main:app --reload\n<\/code><\/pre>\n\n\n\n<p>The server will start on <code>http:\/\/localhost:8000<\/code>. You can visit <code>http:\/\/localhost:8000<\/code> in your browser or send requests using tools like cURL or Postman to test the API endpoints.<\/p>\n\n\n\n<p>This is a basic example to get started with FastAPI. You can add more routes, handle request bodies, use models for request validation, and more as your application grows.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a simple example of a Python FastAPI application: In this example, we create a FastAPI instance called app. We define two routes: the root route (&#8220;\/&#8221;) and an item route (&#8220;\/items\/{item_id}&#8221;). The root route returns a simple JSON response of {&#8220;Hello&#8221;: &#8220;World&#8221;}, while the item route takes an item_id as a path parameter and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,10],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-coding","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python fastapi simple example - Feel Like Learning<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python fastapi simple example - Feel Like Learning\" \/>\n<meta property=\"og:description\" content=\"Here&#8217;s a simple example of a Python FastAPI application: In this example, we create a FastAPI instance called app. We define two routes: the root route (&#8220;\/&#8221;) and an item route (&#8220;\/items\/{item_id}&#8221;). The root route returns a simple JSON response of {&#8220;Hello&#8221;: &#8220;World&#8221;}, while the item route takes an item_id as a path parameter and [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Feel Like Learning\" \/>\n<meta property=\"article:published_time\" content=\"2023-05-13T05:27:05+00:00\" \/>\n<meta name=\"author\" content=\"feellikelearning\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"feellikelearning\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/\",\"url\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/\",\"name\":\"Python fastapi simple example - Feel Like Learning\",\"isPartOf\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/#website\"},\"datePublished\":\"2023-05-13T05:27:05+00:00\",\"dateModified\":\"2023-05-13T05:27:05+00:00\",\"author\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7\"},\"breadcrumb\":{\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/feellikelearning.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python fastapi simple example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#website\",\"url\":\"https:\/\/feellikelearning.com\/en\/\",\"name\":\"Feel Like Learning\",\"description\":\"keep curiosity alive\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/feellikelearning.com\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7\",\"name\":\"feellikelearning\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g\",\"caption\":\"feellikelearning\"},\"sameAs\":[\"http:\/\/feellikelearning.com\/en\"],\"url\":\"https:\/\/feellikelearning.com\/en\/index.php\/author\/feellikelearning\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python fastapi simple example - Feel Like Learning","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/","og_locale":"en_US","og_type":"article","og_title":"Python fastapi simple example - Feel Like Learning","og_description":"Here&#8217;s a simple example of a Python FastAPI application: In this example, we create a FastAPI instance called app. We define two routes: the root route (&#8220;\/&#8221;) and an item route (&#8220;\/items\/{item_id}&#8221;). The root route returns a simple JSON response of {&#8220;Hello&#8221;: &#8220;World&#8221;}, while the item route takes an item_id as a path parameter and [&hellip;]","og_url":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/","og_site_name":"Feel Like Learning","article_published_time":"2023-05-13T05:27:05+00:00","author":"feellikelearning","twitter_card":"summary_large_image","twitter_misc":{"Written by":"feellikelearning","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/","url":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/","name":"Python fastapi simple example - Feel Like Learning","isPartOf":{"@id":"https:\/\/feellikelearning.com\/en\/#website"},"datePublished":"2023-05-13T05:27:05+00:00","dateModified":"2023-05-13T05:27:05+00:00","author":{"@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7"},"breadcrumb":{"@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/feellikelearning.com\/en\/index.php\/2023\/05\/13\/python-fastapi-simple-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/feellikelearning.com\/en\/"},{"@type":"ListItem","position":2,"name":"Python fastapi simple example"}]},{"@type":"WebSite","@id":"https:\/\/feellikelearning.com\/en\/#website","url":"https:\/\/feellikelearning.com\/en\/","name":"Feel Like Learning","description":"keep curiosity alive","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/feellikelearning.com\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/1ec5aac313d6de20215fe2b8e176b8a7","name":"feellikelearning","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/feellikelearning.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/36aec9d519f02362e3e89b0716ae640d08701f57e818830f3f197db5fbc1ae20?s=96&d=mm&r=g","caption":"feellikelearning"},"sameAs":["http:\/\/feellikelearning.com\/en"],"url":"https:\/\/feellikelearning.com\/en\/index.php\/author\/feellikelearning\/"}]}},"_links":{"self":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":1,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":50,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions\/50"}],"wp:attachment":[{"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/feellikelearning.com\/en\/index.php\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}