15 comments

  • tiangolo 1857 days ago
    FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+. It is one of the fastest Python frameworks available, as measured by independent benchmarks.

    It is based on standard Python type hints. Using them, you get automatic data validation, serialization, and documentation. Including deeply nested JSON documents. And you get editor completion and checks everywhere.

    Designed around the OpenAPI (Swagger) and JSON Schema standards.

    It includes 2 alternative interactive API documentation systems that are generated automatically.

    OAuth2 capable from the start (including JWT). Dependency Injection system, compatible with all databases (relational and NoSQL), Websockets, GraphQL, CORS, etc.

    • franey 1856 days ago
      It seems like a lot of the "FastAPI features"[1] are coming not from FastAPI but from Starlette:

      - Just Modern Python

      - Editor support

      - Short

      - Unlimited "plug-ins"

      Would it be fair to say that this is Starlette + an API schema + OAuth2 + data validation?

      [1] https://fastapi.tiangolo.com/features/#fastapi-features

      • tiangolo 1856 days ago
        Yes! Sort of... Most of FastAPI features come from Starlette AND Pydantic.

        In FastAPI you declare the parameters of your request (including deeply nested JSON bodies) as function parameters.

        So, "editor support" here means, for example, autocomplete even for deeply nested models.

        Also, you get automatic data validation and serialization. Automatic detailed errors in your API responses.

        Also, it has a powerful dependency injection system, that helps in a lot of cases.

    • gravypod 1856 days ago
      Do you have any examples of a good project layout? How do I have multiple controllers, large numbers of models, etc. There's very little in the way of "just a restful backend" templates in Python.
  • ilovecaching 1856 days ago
    Another way to get Go like speed is to use Go. Go has an excellent built in http web server, and unlike Python it comes standard with a bug reducing type system and an easy and intuitive syntax. In fact, the entire specification of the language can be read in an hour by a novice programmer.

    Not only that, but Go has best in class editing support in VSCode and Vim, has an official code formatting tool (sorry black), and makes writing concurrent code painless with a syncronous abstraction (sorry asyncio).

    Also it may have good response times in a vacuum but there’s still the Python memory tax, startup times (which matter when you need to scale horizontally), and scaling of latency to think about.

    • tomchristie 1855 days ago
      Python is a hugely productive language. That's the benefit.

      We're building out an async ecosystem here that makes it way more performance-competitive with Node and Go.

      For a lot of folks it's going to hit a sweet spot of developer productivity plus ability to scale up hugely. That's a big deal.

      Doesn't mean you have to use it, but there's no need to neg on the great work folks are doing in this space just because Python's not your preferred language of choice.

    • tiangolo 1856 days ago
      Yeah, the advantage here over Go would be to have automatic data validation, serialization, and documentation, all based on standards: OpenAPI and JSON Schema. Including automatic web user interface for API documentation.

      And maybe the dependency injection system.

      And Python's ecosystem with lots of packages.

      And that Python is the best/main way to do Machine Learning/Deep Learning.

      I personally don't think new asyncio (Python 3.6) is more difficult, but that's more personal taste.

      Indeed, FastAPI inherited benefits from ideas in Go and other languages and frameworks.

      • ilovecaching 1856 days ago
        > Yeah, the advantage here over Go would be to have automatic data validation, serialization, and documentation, all based on standards: OpenAPI and JSON Schema. Including automatic web user interface for API documentation.

        Golang has libraries for all of the things you mentioned, and web tools to generate APIs based on those schemas. There's no advantage here over Go.

        > And maybe the dependency injection system.

        Go has interfaces, which facilitates easy DI. No advantage there, and Go sells itself on not having a magic DI framework like the crap Angular 1 had.

        > And Python's ecosystem with lots of packages.

        Python's ecosystem may have more packages taken as a whole, but Go has a richer web and infrastructure community thanks to being a web first language and growing out of the DevOps community.

        > And that Python is the best/main way to do Machine Learning/Deep Learning.

        Which means nothing. You shouldn't be running ML in your web server. You use grpc or http to make requests to your ML backend, or generate a config for the web server to consume.

        > I personally don't think new asyncio (Python 3.6) is more difficult, but that's more personal taste.

        Even if it isn't more difficult, functions have to be colored async which leads to ecosystem fragmentation. It's a huge pita.

        • lapnitnelav 1856 days ago
          Dude, are you a Go bot written to promote Go?

          Have you considered that maybe not every one wants have anything to do with the 8th wonder of the world that is Go?

          Take it easy pal, let people enjoy things.

          • dr_wolf 1856 days ago
            It looks like some (extremely dedicated) trolling, don't take it seriously. There is certainly nothing wrong in expanding the ecosystem of your preferred language!
            • ilovecaching 1856 days ago
              The way I see it, you're offering no counterpoint and trying to discredit me with a character attack, which makes you the troll.
          • haolez 1855 days ago
            I use Python asyncio a lot and I agree with the parent's opinion.
          • ilovecaching 1856 days ago
            Languages aren't flavors of ice cream. Considering the technical arguments of one technology over the other is part and parcel of our profession, and online forums are we go to have these discussions. The person who wrote this, or the people who might look to consume it might not know that better alternatives exist that can meet their requirements and offer them additional advantages.
            • tiangolo 1856 days ago
              > Golang has libraries for all of the things you mentioned, and web tools to generate APIs based on those schemas. [...]

              Would you cite some? I would love to see the links, packages, etc. And how could you achieve the same in Go:

              Having a single type declaration, get web data validation, serialization, documentation with OpenAPI and automatic clear error responses.

              I would love to see it, because before building FastAPI I looked for it in Go-land a lot too, and didn't find it, not even closer to the tools that already existed in Python.

  • m_ke 1856 days ago
    You should also check out starlette and other supporting projects that Tom Christie (of DRF fame) is working on right now. Both FastAPI and Responder are thin wrappers around starlette.

    Tom Christie has been quietly building what's starting to look like an async first decoupled version of django:

    - https://github.com/encode/starlette

    - https://github.com/encode/orm

    - https://github.com/encode/typesystem

    - https://github.com/encode/databases

    - https://github.com/encode/apistar

    - https://github.com/encode/uvicorn

    • tiangolo 1856 days ago
      Yep, FastAPI is based on Starlette, heavily inspired by the (now "deprecated") APIStar.

      What it adds is automatic data validation, serialization, and documentation, with Python type hints. All using Pydantic.

      So, you declare request parameters and bodies with standard Python type hints (even for deeply nested JSON bodies) and it automatically does all that.

      It also has a simple but powerful dependency injection system.

      And thanks to Tom Christie's work, it is compatible with all those tools too (databases, orm, uvicorn, all from Starlette, etc).

  • zedpm 1856 days ago
    Great documentation! The extra level of detail like explaining the command for running the server is super helpful for less experienced users.
    • svenhof 1854 days ago
      Also love the fact that some of the code samples are unit tested to make sure they are functional.
      • tiangolo 1846 days ago
        Nice observation!

        Yes, most of the tests come directly from the docs, that way, by having full test coverage, means that at least a big part of the functionality is not only tested but also documented somewhere.

    • andres32a 1856 days ago
      I agree, this is what made me fall in love with fastapi.
    • tiangolo 1856 days ago
      That's great to hear!
  • darrenf 1856 days ago
    Looks great; been searching for something to rewrite a 6yo side-project with and this looks just about perfect.

    Small wrinkle: I spotted a broken link on the "deployment" page - "Bigger applications with multiple files" doesn't link to the right place (in fact it links nowhere, the `href` attribute has no value).

    • tiangolo 1856 days ago
      > Looks great; been searching [...]

      That's great!

      > Small wrinkle [...]

      Oops, good catch! Thanks. It's fixed now.

  • darkwinx 1857 days ago
    It's looks great. I've been looking for something like this. I like the documentation. It's very clear.
    • tiangolo 1856 days ago
      That's great to hear!
  • qb 1856 days ago
    How does this compare to https://moltenframework.com/? They both provide type hints and are inspired by API Star. Why should I use FastAPI over molten?
    • tiangolo 1856 days ago
      Molten also helped inspire parts of FastAPI. And they are indeed quite similar in several things.

      Molten is based on WSGI, FastAPI is based on ASGI.

      So, you can do async/await stuff in FastAPI.

      FastAPI inherits directly form Starlette, so it inherits its benefits, like testing tools, GraphQL in-process background tasks, etc.

      And it benefits from the ASGI ecosystem, for example with plugins to handle Django-Channels like Websockets, deployment to "serverless", even including other tools like Socket-IO.

      FastAPI is based on Pydantic instead of an internal library for declaring schemas, so any improvements there are inherited here.

      It supports Flask like route decorators.

      The dependency injection is simpler, probably more intuitive (but this is more subjective). The same with several other parts.

      But again, Molten is a great tool, and was one of the tools that inspired FastAPI (it's even mentioned in the docs).

      • qb 1856 days ago
        Thank you for the detailed answer!
  • wodenokoto 1856 days ago
    For someone who is a little lost when somebody just says "API" ... Is this like flask but without jinja-templating?
    • tiangolo 1856 days ago
      It's designed to be very helpful while building web APIs that communicate JSON over HTTP.

      You can have semi-automatic data validation, serialization (converting from JSON to Python data types, etc). And an automatic web user interface that documents your API. You don't have to do anything to have that web interface, it's done automatically from your definitions of the data types that you need.

      It is also compatible with Jinja templates.

      It's like a modern Flask with several benefits, especially for web APIs.

  • Thaxll 1856 days ago
    When you look at what you need in Python to get semi-decent performance:

    - C library everywhere ( network, serialization ... )

    - server that manages process ect ...

    At some point if you need that kind of things just pickup a language that does it out of the box.

    • linuxftw 1856 days ago
      > C library everywhere ( network, serialization ... )

      I view this as a strong benefit in python. Write your app, and if you need to really squeeze performance in some critical area, writing a C/C++ extension is simple enough.

    • tiangolo 1856 days ago
      Maybe, but Python is still quite easy to pick, the only one that can do Deep Learning and Machine Learning well, etc.

      So, if we can use it, take advantage of its benefits and have tools like this that simplify everything without too many constraints, I would say it's a good tradeoff.

      • m_ke 1856 days ago
        Yeah for data science and CRUD workloads python is great. The performance difference between go and python in those settings are negligible and python wins out on ease of use and ecosystem.
        • tiangolo 1856 days ago
          Exactly. Totally agree.
      • grumpopotamus 1856 days ago
        >Maybe, but Python is still quite easy to pick, the only one that can do Deep Learning and Machine Learning well, etc.

        This is such a terrible state of affairs. There are so many times I've wanted to generate synthetic training data efficiently and iterate in a tight training loop where I've gone back and forth endlessly between trying to get Python to be fast and trying to get the training part to work in another language.

        • tiangolo 1856 days ago
          If the bottleneck is computation, make sure you are using Numpy, Pandas, Dask, and the whole family of scientific Python.

          If it's IO, check asyncio tools. E.g. aiofiles.

    • jniedrauer 1856 days ago
      I'd like to see a more streamlined model for generating swagger files/generating APIs from swagger files in Go, but I mostly agree.
      • tiangolo 1856 days ago
        Yeah, the idea is to take inspiration from Go and similar languages, using the new async (coroutine) features in Python that give it better concurrency handling, and the benefits of types. Without having to deal with a lower-level language.
    • j88439h84 1856 days ago
      Pypy is really fast.
      • jerf 1856 days ago
        Relative to CPython, yes.

        In the grand landscape of programming language performance, it tops out at middling on practical code.

        • tiangolo 1856 days ago
          I guess the tradeoff is speed and convenience.

          If we wanted to have the absolute best performance, we would have to write web apps in C. But it would be quite more cumbersome.

          • jerf 1856 days ago
            It's not a value judgment, which I feel (though I can't prove) you are still reading it is. But it isn't. It just is what it is. The performance characteristics of various technologies just... are. It's a mistake to read too much value judgment into it. I don't "hate" PyPy because it's not as fast as C, or love C because it's fast. They've got all sorts of characteristics, and a professional engineer ought to understand them as dispassionately as possible to make decisions about how to solve problems.

            PyPy is a solution to "my Python code is running slowly". For very little effort you can obtain a very cheap speedup. It may not be all you need, but it's very engineer-effort cheap. But it certainly isn't a solution to "my Python code is running slowly but I need it to run at C speeds", and there are a ton of other solutions to the problem "My Python code is running slowly", since Python is such a slow language on its own terms. (By contrast, "my C code is running slowly and I've already profiled it a lot, etc." is a huge problem.)

            • tiangolo 1856 days ago
              Yep, agree. Thanks for the clarification.
      • tiangolo 1856 days ago
        And Uvicorn, the server, is compatible with PyPy. So, you can run FastAPI with PyPy, if you need :)
  • Splendor 1850 days ago
    This looks awesome. I'll definitely be using this for the next API I build.
  • andres32a 1857 days ago
    Best completion I've ever seen and a breath of fresh air. Love it.
  • rmdashrfstar 1856 days ago
    Why should I use this instead of Falcon?
    • tiangolo 1856 days ago
      To use Python type hints, and get automatic data validation, serialization, and documentation for your API (including interactive UI docs for your API). All that, even for deeply nested JSON bodies. And by using type hints, you get autocomplete everywhere, type error checks, etc.

      Your API gets documented with standards: OpenAPI and JSON Schema.

      Or to be able to have WebSockets.

      Or for its dependency injection system, that saves you a lot of code and plugins.

      You can check the features here: https://fastapi.tiangolo.com/features/

      And you can see alternatives and comparisons here: https://fastapi.tiangolo.com/alternatives/

      • timothycrosley 1856 days ago
        Note that if you are currently using Falcon, another route to get many of these features is to use hug (https://github.com/timothycrosley/hug) which inspired many of API Star features back in the day - and is part of the Falcon ecosystem. But there's also a lot of really amazing work done in this ecosystem that hug doesn't currently support - I may have a busy couple of months ahead :).

        (DISCLAIMER: I am the creator of hug)

        • tiangolo 1856 days ago
          Yep, and Hug was one of the first tools I saw with this style of declaring parameters with types in the functions, along with APIStar.

          Great job! And thanks. It helped inspiring the creation of FastAPI. Apart from other of your tools that I use all the time, like isort.

          An advantage I see of Hug: the same Hug application can be a CLI app.

          A couple advantages I would see of FastAPI (disclaimer, I'm the creator :) ):

          * The API schema uses the standards OpenAPI and JSON Schema * It includes a couple of interactive API documentation web user interfaces (Swagger UI, and ReDoc). Thanks to being OpenAPI. * It uses standard Python type hints, not custom types included as part of the library. * The dependency injection system. * Websockets.

          • timothycrosley 1856 days ago
            Glad to hear that :)!

            Honestly, what you've built looks super solid and polished. In many ways, it's what I wanted hug to be - it's really inspiring to see someone build that.

            • tomchristie 1855 days ago
              Just wanted to second @tiangolo's "Thanks for paving the way with Hug" - Absolutely yup! It's a lovely bit of interface design - definitely had a really positive impact on the space.
            • tiangolo 1856 days ago
              Wow, thanks! I take it as a high compliment. :D

              Thanks for paving the way with Hug. And thanks for helping make the Python ecosystem a friendly, and welcoming community.

  • mahesh_rm 1857 days ago
    Been using it, works great.
  • mlevental 1856 days ago
    honest,sincere question:

    i don't understand why people do this - build these frameworks. it's hundreds of independent labor hours and forgoes the key value prop of python (or whatever language they're reimplementing the wheel in): the existing ecosystem. i would never use this over flask or django because the community around each of those frameworks. that includes plugins, docs, blog posts, SO posts, etc. further more i'm not going to trust that one shop will exist forever and maintain this framework to the same extent that django is maintained. so in effect by adopting this framework i take on the maintenance responsibility. but why would i ever do that? it's not interesting work (very little groundbreaking research left in the http server space i think), it's thankless, and it's just completely unnecessary - every time i submit a PR i'll be looking enviously at django foundation's latest release.

    so why would anyone write something like this and why would anyone ever adopt? if you're going for speed without ecosystem then just use Go or Java or C# to begin with. is it just NIH syndrome?

    • tiangolo 1856 days ago
      About reinventing the wheel, let me copy my answer from Reddit:

      Good question. I ask the same questions myself every time. And I avoided creating FastAPI for a long time.

      I tried a whole lot of tools. And FastAPI got inspiration from a lot of them.

      A more detailed explanation is here: https://fastapi.tiangolo.com/alternatives/

      More details about Flask and Django:

      About Flask:

      I created the mosts popular Docker images for Flask. I've been building APIs with Flask for a long time, with several teams, using many of its plug-ins, etc. All that learning was inherited by FastAPI. To reduce development time and effort.

      About Django:

      If you are building APIs with Django, chances are you are using Django-REST-Framework. It was built by Tom Christie. He then built APIStar, improving over what Django-REST-Framework could do. Then he had to stop building APIStar to dedicate himself to Starlette, the new super-fast microframework. FastAPI is heavily inspired by APIStar, and based on Starlette. And uses Python type hints even more.

      About plug-ins:

      If you refer to plugins to do: authentication, data validation, data serialization, documentation, automatic API docs, compatibility with all SQL and NoSQL, dependency injection, Websockets, Testing, GraphQL... it's all included. Even though it's a mini-framework. Most of the functionality that is normally achieved with plugins is included.

      Also, it's heavily integrated into the ASGI ecosystem, so, you have plug-ins to handle Django-Channels like Websockets, serverless deployments, Socket-IO, etc.

      Give it from 5 to 20 minutes. That's enough to decide if you like it or hate it.

    • tomchristie 1855 days ago
      There's a pretty good reason there's a lot of folks building new frameworks for Python in this space right now: async.

      Unlike WSGI frameworks we're now able to support WebSockets, lightweight backgrounds tasks, super-high-throughput performance, and all sorts of other goodies, that Flask, Django et al. aren't necessarily able to excel in.

      Good news is that it's pretty likely that some of this may eventually feedback into Django, Flask, and others, but in the meantime it's a case of trying to bring the Python async ecosystem up to feature-parity with existing thread-concurrency frameworks as quickly as we possibly can.

      • tiangolo 1855 days ago
        Thanks for chiming in @tomchristie!
    • heavenlyblue 1856 days ago
      Simply because any sufficiently old Django app has the same intro curve as any other (possibly customised) framework.

      Have you actually used Flask, by the way? Or the older version of it called Pyramid? The truth with it is that it's not only complex, but also doesn't prefer any of it's optional components. Any project you'll be building in it is going to be custom anyway.

      • piquadrat 1856 days ago
        > Have you actually used Flask, by the way? Or the older version of it called Pyramid?

        Flask is in no way related to Pyramid. It came out of an April Fools joke in 2010 (http://lucumr.pocoo.org/2010/4/3/april-1st-post-mortem/). Pyramid, OTOH comes out of the Plone/Zope communities (it used to be known as repoze.bfg).

      • tiangolo 1856 days ago
        Yeah, in fact, up to FastAPI, my main stack was based on Flask with several plug-ins, and I created several project generators with all that.

        FastAPI learned a lot from many of these tools and techniques.

    • linuxftw 1856 days ago
      I wrote a web (WSGI) framework in python because I didn't care for the abstractions of some of the larger ones.

      It was a good bit of fun, and it was faster than anything else at the time. Much of the speed came from doing less out of the box. Additionally, it didn't attempt to handle any of the actual connection layer. Everything is done via the WSGI interface, which I think is a great abstraction.

      • tiangolo 1856 days ago
        Yep, this is based on Starlette, which does more or less, what you did, but for ASGI.
    • franey 1856 days ago
      FastAPI extends Starlette, so the developer didn't build an entirely new framework from scratch.

      Encode, the team that develops Starlette, also develops Django REST Framework. They help make the Python web development ecosystem what it is.

      • tiangolo 1856 days ago
        Exactly! FastAPI inherits a lot from Tom Christie's great work.

        Even the heaviest inspiration is form APIStar (now "deprecated" as a server). Built by him too.