{"id":21672546,"url":"https://github.com/redis-developer/fastapi-redis-tutorial","last_synced_at":"2025-05-05T18:47:09.948Z","repository":{"id":43384814,"uuid":"371537895","full_name":"redis-developer/fastapi-redis-tutorial","owner":"redis-developer","description":"Using Redis with FastAPI","archived":false,"fork":false,"pushed_at":"2024-07-01T20:55:51.000Z","size":155,"stargazers_count":115,"open_issues_count":0,"forks_count":87,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-30T22:51:14.862Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redis-developer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-28T00:34:51.000Z","updated_at":"2025-03-14T13:22:18.000Z","dependencies_parsed_at":"2023-02-16T17:31:43.626Z","dependency_job_id":null,"html_url":"https://github.com/redis-developer/fastapi-redis-tutorial","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Ffastapi-redis-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Ffastapi-redis-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Ffastapi-redis-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redis-developer%2Ffastapi-redis-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redis-developer","download_url":"https://codeload.github.com/redis-developer/fastapi-redis-tutorial/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252555967,"owners_count":21767267,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-25T13:29:59.632Z","updated_at":"2025-05-05T18:47:09.896Z","avatar_url":"https://github.com/redis-developer.png","language":"Python","readme":"# FastAPI Redis Example\n\nThis is an example API that demonstrates how to use Redis with FastAPI to build\na fully async web service in Python.\n\nThe API is called *IsBitcoinLit*. Readers outside the U.S. who are unfamiliar with\nthe slang term \"lit\" might enjoy this [Merriam-Webster\netymology](https://www.merriam-webster.com/words-at-play/lit-meaning-origin#:~:text=Lit%20has%20been%20used%20as,is%20%22exciting%20or%20excellent.%22).\n\nThe IsBitcoinLit API tracks Bitcoin sentiment and prices over time, rolling\nthese up into hourly averages of averages using the [RedisTimeSeries\nmodule](https://oss.redislabs.com/redistimeseries/). You can use the API to get\naverage Bitcoin price and sentiment for each of the last three hours, with a\nquick indication of price and sentiment movement.\n\n\n## Setup\n\nThis project is designed to run as a set of Docker containers. You will need to\n[install Docker](https://www.docker.com/) to complete the setup tasks.\n\nFirst, clone this repo and build the Docker images for the project:\n\n    $ git clone https://github.com/redis-developer/fastapi-redis-tutorial.git\n    $ cd fastapi-redis-tutorial\n    $ docker-compose build\n\nRunning the API involves starting the app server and Redis. You'll do those steps\nnext!\n\n\n## Running the API\n\nThe `docker-compose.yaml` file in this project configures containers for a Redis\ninstance with the RedisTimeSeries module, the Python app for the example API,\nand a test runner.\n\nUse this command to run all three containers:\n\n    $ docker-compose up\n\nThis command starts Redis and the API server and runs the tests.\n\n\n### Ingesting Price and Sentiment Data\n\nA `/refresh` endpoint exists in the app that ingests the last 24 hours of\nBitcoin price and sentiment data.\n\nThe app assumes a scheduler (cron job, Google Cloud Scheduler, etc.) will hit\nthe `/refresh` endpoint on a regular basis to keep data fresh.\n\n**NOTE** : We can refresh data as often as we want without getting\nduplicate data. This is because RedisTimeSeries allows [configuring\nrules](https://oss.redislabs.com/redistimeseries/configuration/#duplicate_policy)\nto ignore duplicate sample and timestamp pairs.\n\nAfter you first start the API, use the `/refresh` API to ingest data:\n\n    $ curl -X POST localhost:8080/refresh\n\nNow you can use the `/is-bitcoin-lit` endpoint to see a summary of Bitcoin price\nand sentiment data. Continue reading to see how to use that endpoint.\n\n**NOTE**: We've used the free [SentiCrypt](https://senticrypt.com) API to pull\nBitcoin sentiment and price. We are not affiliated with SentiCrypt and this is **in no way**\na recommendation to use the API for crypto price and sentiment tracking in real applications.\n\n\n### Getting Summary Price and Sentiment Data from the API\n\nUse the `/is-bitcoin-lit` endpoint to get an hourly summary of Bitcoin price and\nsentiment data for the last three hours:\n\n    $ curl localhost:8080/is-bitcoin-lit | jq\n\n```json\n    {\n    \"hourly_average_of_averages\": [\n        {\n        \"price\": \"32928.345\",\n        \"sentiment\": \"0.22\",\n        \"time\": \"2021-07-08T17:00:00+00:00\"\n        },\n        {\n        \"price\": \"32834.2910891089\",\n        \"sentiment\": \"0.224257425742574\",\n        \"time\": \"2021-07-08T18:00:00+00:00\"\n        },\n        {\n        \"price\": \"32871.3406666667\",\n        \"sentiment\": \"0.208666666666667\",\n        \"time\": \"2021-07-08T19:00:00+00:00\"\n        }\n    ],\n    \"sentiment_direction\": \"rising\",\n    \"price_direction\": \"rising\"\n    }\n```\n\nAs you can see, the response includes the key `hourly_average_of_averages`. This\nkey contains hourly averages derived from the Bitcoin sentiment API's data,\nwhich is itself averaged over 30-second periods. (Thus, these are *averages of\naverages*.)\n\nThe API also returns the *direction* that the price and sentiment are moving.\nThe directions are:\n\nValue  | Meaning\n---------|----------\nRising | The price has risen over the past three hours.\nFalling | The price has fallen over the past three hours.\nNeutral | The price stayed the same for three hours (unlikely!)\n\nSo, *is Bitcoin lit* in this example? Yes, it's lit: the price and sentiment are\nrising. See how that works?\n\n\n### Running Tests\n\nYou can run the app's test suite using docker-compose:\n\n    $ docker-compose up test\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis-developer%2Ffastapi-redis-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredis-developer%2Ffastapi-redis-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredis-developer%2Ffastapi-redis-tutorial/lists"}