{"id":382,"date":"2024-03-02T23:02:49","date_gmt":"2024-03-02T17:32:49","guid":{"rendered":"https:\/\/mrcoder701.com\/?p=382"},"modified":"2024-03-15T16:55:36","modified_gmt":"2024-03-15T11:25:36","slug":"create-microservices-fastapi-guide","status":"publish","type":"post","link":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/","title":{"rendered":"How to Create Microservices with FastAPI"},"content":{"rendered":"

Ah, microservices! They’re like the Lego blocks of the programming world\u2014small, manageable, and incredibly versatile. And when you’re crafting these tiny titans, what better tool to reach for than FastAPI? It’s fast (duh!), it’s pythonic, and it’s got swagger (literally, with automatic API documentation). If you’re itching to break your monolith into microservices or just looking to dip your toes into the microservice architecture, you’re in the right place. Buckle up as we dive deep into creating microservices with FastAPI. We’ve got step-by-step instructions and examples that’ll have you up and running in no time. Ready to become a microservices maestro? Let’s get cracking!<\/p>

Step 1: Setting Up Your Environment<\/h3>

First things first, you’ll need Python installed on your machine (Python 3.6+ is required for FastAPI). If you haven’t got it yet, head over to the Python website and follow the installation instructions for your operating system.<\/p>

Before installing FastAPI create a new directory\u00a0fast_examples<\/code>\u00a0and create a new virtual environment inside the newly created directory using\u00a0virtualenv<\/a>.
If you haven’t already installed\u00a0virtualenv<\/code>:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
pip<\/span> <\/span>install<\/span> <\/span>virtualenv<\/span><\/span><\/code><\/pre><\/div>

Now, create a new virtual environment.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
virtualenv<\/span> <\/span>env<\/span><\/span><\/code><\/pre><\/div>

If you are on Mac\/Linux you can activate the virtual environment using the command:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
source<\/span> <\/span>.<\/span>\/<\/span>env<\/span>\/<\/span>bin<\/span>\/<\/span>activate<\/span><\/span><\/code><\/pre><\/div>

Windows users can run this command instead:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
.<\/span>\\<\/span>env<\/span>\\<\/span>Scripts<\/span>\\<\/span>activate<\/span><\/span><\/code><\/pre><\/div>

<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
pip<\/span> <\/span>install<\/span> <\/span>fastapi<\/span> <\/span>uvicorn<\/span><\/span><\/code><\/pre><\/div>

Step 2: Your First FastAPI Microservice<\/h3>

Now, let’s get our hands dirty by writing our first microservice. Create a file named main.py<\/code> in your project directory and open it in your favorite text editor. Paste in the following code:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
from<\/span> <\/span>fastapi<\/span> <\/span>import<\/span> <\/span>FastAPI<\/span><\/span>\n<\/span>\napp<\/span> = <\/span>FastAPI<\/span>()<\/span><\/span>\n<\/span>\n@<\/span>app<\/span>.<\/span>get<\/span>(<\/span>"<\/span>\/<\/span>"<\/span>)<\/span><\/span>\ndef<\/span> <\/span>read_root<\/span>():<\/span><\/span>\n    <\/span>return<\/span> <\/span>{<\/span>"<\/span>Hello<\/span>": "<\/span>World<\/span>"<\/span>}<\/span><\/span><\/code><\/pre><\/div>

This snippet creates a basic FastAPI app that responds with {\"Hello\": \"World\"}<\/code> when you navigate to the root URL. To fire up your server and see this in action, run:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
uvicorn<\/span> main<\/span>:<\/span>app<\/span> <\/span>--<\/span>reload<\/span><\/span><\/code><\/pre><\/div>

Visit http:\/\/127.0.0.1:8000<\/code> in your browser, and voil\u00e0, your first microservice greets you.<\/p>

Step 3: Adding More Endpoints<\/h3>

Microservices often need to handle various tasks. Let’s add another endpoint to our service that echoes back whatever is passed to it. Add the following code to your main.py<\/code>:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
@<\/span>app<\/span>.<\/span>get<\/span>(<\/span>"<\/span>\/{name}<\/span>"<\/span>)<\/span><\/span>\ndef<\/span> <\/span>read_item<\/span>(<\/span>name<\/span>: <\/span>str<\/span>):<\/span><\/span>\n    <\/span>return<\/span> <\/span>{<\/span>"<\/span>Hello<\/span>"<\/span>:<\/span> <\/span>name<\/span>}<\/span><\/span><\/code><\/pre><\/div>

Now, if you navigate to http:\/\/127.0.0.1:8000\/yourname<\/code>, replacing yourname<\/code> with anything you like, the service will greet you by name.<\/p>

Step 4: Using Path and Query Parameters<\/h3>

FastAPI makes it super easy to work with both path parameters (like the {name}<\/code> we just used) and query parameters. Let’s add an endpoint that uses both. Add this to your main.py<\/code>:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
@<\/span>app<\/span>.<\/span>get<\/span>(<\/span>"<\/span>\/items\/{item_id}<\/span>"<\/span>)<\/span><\/span>\ndef<\/span> <\/span>read_item<\/span>(<\/span>item_id<\/span>: <\/span>int<\/span>,<\/span> <\/span>q<\/span>: <\/span>str<\/span> <\/span>=<\/span> <\/span>None<\/span>):<\/span><\/span>\n    <\/span>return<\/span> <\/span>{<\/span>"<\/span>item_id<\/span>"<\/span>:<\/span> <\/span>item_id<\/span>,<\/span> <\/span>"<\/span>q<\/span>"<\/span>:<\/span> <\/span>q<\/span>}<\/span><\/span><\/code><\/pre><\/div>

Now, accessing http:\/\/127.0.0.1:8000\/items\/3?q=somequery<\/code> will show you how FastAPI handles both path parameters (item_id<\/code>) and query parameters (q<\/code>).<\/p>

Step 5: Adding a Post Endpoint<\/h3>

Microservices often need to receive data, not just send it. Let’s create an endpoint to post data to our service. Update your main.py<\/code> with:<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
from<\/span> <\/span>fastapi<\/span> <\/span>import<\/span> <\/span>FastAPI<\/span>,<\/span> <\/span>HTTPException<\/span><\/span>\nfrom<\/span> <\/span>pydantic<\/span> <\/span>import<\/span> <\/span>BaseModel<\/span><\/span>\n<\/span>\nclass<\/span> <\/span>Item<\/span>(<\/span>BaseModel<\/span>):<\/span><\/span>\n    <\/span>name<\/span>: <\/span>str<\/span><\/span>\n    <\/span>description<\/span>: <\/span>str<\/span> = <\/span>None<\/span><\/span>\n    <\/span>price<\/span>: <\/span>float<\/span><\/span>\n    <\/span>tax<\/span>: <\/span>float<\/span> = <\/span>None<\/span><\/span>\n<\/span>\n@<\/span>app<\/span>.<\/span>post<\/span>(<\/span>"<\/span>\/items\/<\/span>"<\/span>)<\/span><\/span>\ndef<\/span> <\/span>create_item<\/span>(<\/span>item<\/span>: <\/span>Item<\/span>):<\/span><\/span>\n    <\/span>return<\/span> <\/span>{<\/span>"<\/span>name<\/span>": <\/span>item<\/span>.<\/span>name<\/span>,<\/span> "<\/span>price<\/span>": <\/span>item<\/span>.<\/span>price<\/span>}<\/span><\/span><\/code><\/pre><\/div>

This code snippet introduces a new concept: Pydantic models. These allow for easy data validation and settings management using Python type annotations.<\/p>

Step 6: Testing Your Endpoints<\/h3>

Testing is crucial in software development, and FastAPI provides built-in support for testing with PyTest. Create a file named test_main.py<\/code> and add some basic tests to ensure your endpoints are working as expected.<\/p>

<\/circle><\/circle><\/circle><\/g><\/svg><\/span><\/path><\/path><\/svg><\/span>
from<\/span> <\/span>fastapi<\/span>.<\/span>testclient<\/span> <\/span>import<\/span> <\/span>TestClient<\/span><\/span>\nfrom<\/span> .<\/span>main<\/span> <\/span>import<\/span> <\/span>app<\/span><\/span>\n<\/span>\nclient<\/span> = <\/span>TestClient<\/span>(<\/span>app<\/span>)<\/span><\/span>\n<\/span>\ndef<\/span> <\/span>test_read_main<\/span>():<\/span><\/span>\n    <\/span>response<\/span> = <\/span>client<\/span>.<\/span>get<\/span>(<\/span>"<\/span>\/<\/span>"<\/span>)<\/span><\/span>\n    <\/span>assert<\/span> <\/span>response<\/span>.<\/span>status_code<\/span> == 200<\/span><\/span>\n    <\/span>assert<\/span> <\/span>response<\/span>.<\/span>json<\/span>() == <\/span>{<\/span>"<\/span>Hello<\/span>": "<\/span>World<\/span>"<\/span>}<\/span><\/span><\/code><\/pre><\/div>

Run your tests with pytest<\/code> to make sure everything is shipshape.<\/p>

Conclusion: The World of Microservices Awaits<\/h3>

And there you have it, folks\u2014a basic but fully functional microservice built with FastAPI. You’ve seen how to set up your environment, create endpoints, work with parameters, accept data, and even write a simple test. But this is just the tip of the iceberg. FastAPI’s documentation is packed with more advanced features like dependency injection, security, and database integration that can help you build robust, efficient microservices.<\/p>

Remember, the journey to microservice mastery is a marathon, not a sprint. Keep experimenting, keep learning, and most importantly, keep coding. The world of microservices is vast and varied, and it’s yours for the taking. Happy coding!<\/p>

Leave a response to this article by providing your insights, comments, or requests for future articles.<\/strong><\/p>

Share the articles with your friends and colleagues on social media.<\/strong><\/p>","protected":false},"excerpt":{"rendered":"

Discover the power of FastAPI in building lightning-fast microservices with our step-by-step guide, complete with real-world examples and pro tips.<\/p>\n","protected":false},"author":2,"featured_media":383,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[116,17],"tags":[20,21,18,19,52,94,93,32,31],"class_list":["post-382","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-fast-api","category-programming","tag-google","tag-medium","tag-python","tag-python3","tag-flask","tag-flaskmicsroservice","tag-microservice","tag-programmingtips","tag-pythondev"],"yoast_head":"\nHow to Create Microservices with FastAPI - 🌟Code with MrCoder7️⃣0️⃣1️⃣ How to Create Microservices with FastAPI<\/title>\n<meta name=\"description\" content=\"Dive into the world of microservices with our comprehensive guide on FastAPI. Learn how to build, deploy, and scale your microservices with easy-to-follow steps and practical examples\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Create Microservices with FastAPI - 🌟Code with MrCoder7️⃣0️⃣1️⃣ How to Create Microservices with FastAPI\" \/>\n<meta property=\"og:description\" content=\"Dive into the world of microservices with our comprehensive guide on FastAPI. Learn how to build, deploy, and scale your microservices with easy-to-follow steps and practical examples\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"🌟Code with MrCoder7️⃣0️⃣1️⃣\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-02T17:32:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-15T11:25:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2240\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"mr.coder\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"mr.coder\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/\"},\"author\":{\"name\":\"mr.coder\",\"@id\":\"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d\"},\"headline\":\"How to Create Microservices with FastAPI\",\"datePublished\":\"2024-03-02T17:32:49+00:00\",\"dateModified\":\"2024-03-15T11:25:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/\"},\"wordCount\":687,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d\"},\"image\":{\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png\",\"keywords\":[\"#google\",\"#medium\",\"#python\",\"#python3\",\"flask\",\"flaskmicsroservice\",\"microservice\",\"ProgrammingTips\",\"PythonDev\"],\"articleSection\":[\"FastAPI\",\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/\",\"url\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/\",\"name\":\"How to Create Microservices with FastAPI - 🌟Code with MrCoder7️⃣0️⃣1️⃣ How to Create Microservices with FastAPI\",\"isPartOf\":{\"@id\":\"https:\/\/www.mrcoder701.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png\",\"datePublished\":\"2024-03-02T17:32:49+00:00\",\"dateModified\":\"2024-03-15T11:25:36+00:00\",\"description\":\"Dive into the world of microservices with our comprehensive guide on FastAPI. Learn how to build, deploy, and scale your microservices with easy-to-follow steps and practical examples\",\"breadcrumb\":{\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage\",\"url\":\"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png\",\"contentUrl\":\"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png\",\"width\":2240,\"height\":1260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.mrcoder701.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Create Microservices with FastAPI\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.mrcoder701.com\/#website\",\"url\":\"https:\/\/www.mrcoder701.com\/\",\"name\":\"Blog With MrCoder701\",\"description\":\"Blog related to programming\",\"publisher\":{\"@id\":\"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.mrcoder701.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d\",\"name\":\"mr.coder\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.mrcoder701.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/06\/369B947D-A5EE-4B16-816A-5EE55D1DDF96_L0_001-10_6_2024-6-13-24-PM.png\",\"contentUrl\":\"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/06\/369B947D-A5EE-4B16-816A-5EE55D1DDF96_L0_001-10_6_2024-6-13-24-PM.png\",\"width\":500,\"height\":500,\"caption\":\"mr.coder\"},\"logo\":{\"@id\":\"https:\/\/www.mrcoder701.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/mrcoder701.com\",\"https:\/\/www.instagram.com\/mr_coder_701\/\",\"https:\/\/www.youtube.com\/@mrcoder701\"],\"url\":\"https:\/\/www.mrcoder701.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Create Microservices with FastAPI - 🌟Code with MrCoder7️⃣0️⃣1️⃣ How to Create Microservices with FastAPI","description":"Dive into the world of microservices with our comprehensive guide on FastAPI. Learn how to build, deploy, and scale your microservices with easy-to-follow steps and practical examples","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:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/","og_locale":"en_US","og_type":"article","og_title":"How to Create Microservices with FastAPI - 🌟Code with MrCoder7️⃣0️⃣1️⃣ How to Create Microservices with FastAPI","og_description":"Dive into the world of microservices with our comprehensive guide on FastAPI. Learn how to build, deploy, and scale your microservices with easy-to-follow steps and practical examples","og_url":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/","og_site_name":"🌟Code with MrCoder7️⃣0️⃣1️⃣","article_published_time":"2024-03-02T17:32:49+00:00","article_modified_time":"2024-03-15T11:25:36+00:00","og_image":[{"width":2240,"height":1260,"url":"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png","type":"image\/png"}],"author":"mr.coder","twitter_card":"summary_large_image","twitter_misc":{"Written by":"mr.coder","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#article","isPartOf":{"@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/"},"author":{"name":"mr.coder","@id":"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d"},"headline":"How to Create Microservices with FastAPI","datePublished":"2024-03-02T17:32:49+00:00","dateModified":"2024-03-15T11:25:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/"},"wordCount":687,"commentCount":2,"publisher":{"@id":"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d"},"image":{"@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png","keywords":["#google","#medium","#python","#python3","flask","flaskmicsroservice","microservice","ProgrammingTips","PythonDev"],"articleSection":["FastAPI","Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/","url":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/","name":"How to Create Microservices with FastAPI - 🌟Code with MrCoder7️⃣0️⃣1️⃣ How to Create Microservices with FastAPI","isPartOf":{"@id":"https:\/\/www.mrcoder701.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage"},"image":{"@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png","datePublished":"2024-03-02T17:32:49+00:00","dateModified":"2024-03-15T11:25:36+00:00","description":"Dive into the world of microservices with our comprehensive guide on FastAPI. Learn how to build, deploy, and scale your microservices with easy-to-follow steps and practical examples","breadcrumb":{"@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#primaryimage","url":"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png","contentUrl":"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png","width":2240,"height":1260},{"@type":"BreadcrumbList","@id":"https:\/\/www.mrcoder701.com\/2024\/03\/02\/create-microservices-fastapi-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mrcoder701.com\/"},{"@type":"ListItem","position":2,"name":"How to Create Microservices with FastAPI"}]},{"@type":"WebSite","@id":"https:\/\/www.mrcoder701.com\/#website","url":"https:\/\/www.mrcoder701.com\/","name":"Blog With MrCoder701","description":"Blog related to programming","publisher":{"@id":"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mrcoder701.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.mrcoder701.com\/#\/schema\/person\/ba1cd6b2ad26df384b1a655341eaef5d","name":"mr.coder","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mrcoder701.com\/#\/schema\/person\/image\/","url":"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/06\/369B947D-A5EE-4B16-816A-5EE55D1DDF96_L0_001-10_6_2024-6-13-24-PM.png","contentUrl":"https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/06\/369B947D-A5EE-4B16-816A-5EE55D1DDF96_L0_001-10_6_2024-6-13-24-PM.png","width":500,"height":500,"caption":"mr.coder"},"logo":{"@id":"https:\/\/www.mrcoder701.com\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/mrcoder701.com","https:\/\/www.instagram.com\/mr_coder_701\/","https:\/\/www.youtube.com\/@mrcoder701"],"url":"https:\/\/www.mrcoder701.com\/author\/admin\/"}]}},"rttpg_featured_image_url":{"full":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png",2240,1260,false],"landscape":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png",2240,1260,false],"portraits":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png",2240,1260,false],"thumbnail":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5-150x150.png",150,150,true],"medium":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5-300x169.png",300,169,true],"large":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5-1024x576.png",1024,576,true],"1536x1536":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5-1536x864.png",1536,864,true],"2048x2048":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5-2048x1152.png",2048,1152,true],"woocommerce_thumbnail":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5-300x300.png",300,300,true],"woocommerce_single":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5.png",600,338,false],"woocommerce_gallery_thumbnail":["https:\/\/www.mrcoder701.com\/wp-content\/uploads\/2024\/03\/Wardiere-In.-5-150x150.png",150,150,true]},"rttpg_author":{"display_name":"mr.coder","author_link":"https:\/\/www.mrcoder701.com\/author\/admin\/"},"rttpg_comment":3,"rttpg_category":"<a href=\"https:\/\/www.mrcoder701.com\/category\/fast-api\/\" rel=\"category tag\">FastAPI<\/a> <a href=\"https:\/\/www.mrcoder701.com\/category\/programming\/\" rel=\"category tag\">Programming<\/a>","rttpg_excerpt":"Discover the power of FastAPI in building lightning-fast microservices with our step-by-step guide, complete with real-world examples and pro tips.","_links":{"self":[{"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/comments?post=382"}],"version-history":[{"count":1,"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/posts\/382\/revisions\/384"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/media\/383"}],"wp:attachment":[{"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mrcoder701.com\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}

We’re going to install FastAPI and an ASGI server, Uvicorn, which will serve as our lightning-fast conduit to the web. Since FastAPI doesn’t come with inbuilt service, you need to install\u00a0uvicorn<\/code>\u00a0for it to run.\u00a0uvicorn<\/code>\u00a0is an\u00a0ASGI<\/a>\u00a0server which allows us to use async\/await features. Run the following command:<\/p>