DEV Community

Yashasvi Singh
Yashasvi Singh

Posted on • Originally published at aunicorndev.hashnode.dev

Supa-Fast API (Supabase FastAPI) : Deploying on DETA

In this last part of the tutorial of the series,we will be looking at deploying the FastAPI that we build in the last tutorial.

We have our Supa-Fast API working on our local system.. But will we be able to deploy it easily????

Deployment  is Fast

To my surprise, DETA has made it relatively easy to deploy the API..

I hit some roadblocks while deploying (OBVIOUSLYYYY) and shared a trick that might work for you like it did for me.

About Deta

As stated on their official website docs,

Deta is a free cloud crafted with the developer and user experience at heart.

What about Pricing??

It's FREE.FOREVER.

Yes, their motto states

We want anyone, at any age from anywhere in the world to experiment and build their ideas without worrying about limits of credit cards.


Prepare your Local Environment

If you have been following the previous tutorials, we have been using virtualenv and have only a limited dependencies in our virtualenv

To get the list of installed dependencies for the project , go to the api directory and run the command

pip freeze > requirements.txt
Enter fullscreen mode Exit fullscreen mode

This will create a requirements.txt file in the api directory.

#requirements.txt

anyio==3.4.0
asgiref==3.4.1
atomicwrites==1.4.0
attrs==21.2.0
certifi==2021.10.8
chardet==4.0.0
charset-normalizer==2.0.8
click==8.0.3
colorama==0.4.4
dataclasses==0.6
deprecation==2.1.0
fastapi==0.70.0
gotrue==0.2.0
h11==0.12.0
httpcore==0.13.7
httptools==0.2.0
httpx==0.19.0
idna==2.10
importlib-metadata==4.8.2
iniconfig==1.1.1
packaging==21.3
pluggy==1.0.0
postgrest-py==0.5.0
py==1.11.0
pydantic==1.8.2
pyparsing==3.0.6
pytest==6.2.5
python-dateutil==2.8.2
python-dotenv==0.19.2
PyYAML==6.0
realtime-py==0.1.3
requests==2.25.1
rfc3986==1.5.0
six==1.16.0
sniffio==1.2.0
starlette==0.16.0
supabase==0.0.3
toml==0.10.2
typing_extensions==4.0.0
urllib3==1.26.7
uvicorn==0.15.0
watchgod==0.7
websockets==9.1
zipp==3.6.0
Enter fullscreen mode Exit fullscreen mode

Make sure you have the .env file with the environment variables as the API won't get deployed in case you missed that step in the last tutorial.

Working with DETA

To start, install the DETA CLI in powershell and login into deta using the following commands..

//The below command for install is for Windows

iwr https://get.deta.dev/cli.ps1 -useb | iex 

deta login


Enter fullscreen mode Exit fullscreen mode

To get the list of available commands, run deta --help

We'll create a new deta micro for the supafast-api after navigating to the api folder from the last tutorial

deta new
Enter fullscreen mode Exit fullscreen mode
// Response on running deta new command

Successfully created a new micro
{
        "name": "api",
        "id": "XXXX_XXXXX_XXXXX_XXXX_XXXX_XXXX",
        "project": "XXXXXXX",
        "runtime": "python3.9",
        "endpoint": "XXXXXXXXXX",
        "region": "XXXXXXXX",
        "visor": "enabled",
        "http_auth": "disabled"
}
Enter fullscreen mode Exit fullscreen mode

The above command will install the dependencies listed in the requirements.txt on our new micro and will be visible on the Deta web interface.

Installed Packages List

In the previous tutorial, we added a .env file for the local environment, but for this created Deta instance, we don't have any environment variable defined. To solve that, run the following command while in the api directory.

deta update -e .env
Enter fullscreen mode Exit fullscreen mode

And our API is available at https://e5w1sl.deta.dev/docs to check out 🤝🤝.

Deta Dashboard

Your API url can be found on the DETA Dashboard as above.

If you want to open up the endpoint to public, run the command

deta auth disable

//Response from CLI

Successfully disabled http auth
Enter fullscreen mode Exit fullscreen mode

Not Getting Any OUTPUT ?????

If you face an error (like me) on running your API in the browser with a response :

Bad Gateway
The micro ran into an error while processing the request.

If you are the owner of the micro, please check your logs.

If the response took around 10s, it might be due to the time out limit.
Enter fullscreen mode Exit fullscreen mode

Run deta logs and try if you can find any error in the logs..

For example, this is the error I got in my logs

[ERROR] AttributeError: module 'typing' has no attribute '_ClassVar'
Traceback (most recent call last):
  File "/var/lang/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/var/task/_entry.py", line 1, in <module>
    from detalib.handler import handle
  File "/opt/python/detalib/handler.py", line 3, in <module>
    from . import handlers
  File "/opt/python/detalib/handlers.py", line 3, in <module>
    from .adapters.asgi import Asgi
  File "/opt/python/detalib/adapters/asgi/__init__.py", line 1, in <module>
    from .adapter import Asgi  # noqa
  File "/opt/python/detalib/adapters/asgi/adapter.py", line 7, in <module>
    from .lifespan import Lifespan
  File "/opt/python/detalib/adapters/asgi/lifespan.py", line 9, in <module>
    class Lifespan:
  File "/opt/python/dataclasses.py", line 958, in dataclass
    return wrap(_cls)
  File "/opt/python/dataclasses.py", line 950, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash, frozen)
  File "/opt/python/dataclasses.py", line 800, in _process_class
    cls_fields = [_get_field(cls, name, type)
  File "/opt/python/dataclasses.py", line 800, in <listcomp>
    cls_fields = [_get_field(cls, name, type)
  File "/opt/python/dataclasses.py", line 659, in _get_field
    if (_is_classvar(a_type, typing)
  File "/opt/python/dataclasses.py", line 550, in _is_classvar
    return type(a_type) is typing._ClassVar
[2021-11-11T00:32:56+05:30] Unknown application error occurred
Enter fullscreen mode Exit fullscreen mode

For this particular error, the problem is with the dataclasses package in the requirements and we will remove it to get the correct output.

For that, run the following commands

pip uninstall dataclasses

pip freeze >requirements.txt

//run this in powershell
deta deploy
Enter fullscreen mode Exit fullscreen mode

Now on running the Deta Micro with the URL (https://e5w1sl.deta.dev/themes)

// 20211130191258
// https://e5w1sl.deta.dev/themes

{
  "data": [
    {
      "id": 1,
      "created_at": "2021-11-26T15:28:21+00:00",
      "monsterTheme": "demo-theme-1",
      "monsterThemeBg": "https://jtpcokcjjqbizziufohl.supabase.co/storage/v1/object/public/supafast-api/themes/demo-theme-1.png"
    },
    {
      "id": 2,
      "created_at": "2021-11-26T15:29:45+00:00",
      "monsterTheme": "demo-theme-2",
      "monsterThemeBg": "https://jtpcokcjjqbizziufohl.supabase.co/storage/v1/object/public/supafast-api/themes/demo-theme-2.png"
    },
    {
      "id": 3,
      "created_at": "2021-11-26T15:30:04+00:00",
      "monsterTheme": "demo-theme-3",
      "monsterThemeBg": "https://jtpcokcjjqbizziufohl.supabase.co/storage/v1/object/public/supafast-api/themes/demo-theme-3.png"
    },
    {
      "id": 4,
      "created_at": "2021-11-26T15:30:12+00:00",
      "monsterTheme": "demo-theme-4",
      "monsterThemeBg": "https://jtpcokcjjqbizziufohl.supabase.co/storage/v1/object/public/supafast-api/themes/demo-theme-4.png"
    },
    {
      "id": 6,
      "created_at": "2021-11-26T15:30:21+00:00",
      "monsterTheme": "demo-theme-5",
      "monsterThemeBg": "https://jtpcokcjjqbizziufohl.supabase.co/storage/v1/object/public/supafast-api/themes/demo-theme-5.png"
    }
  ],
  "status_code": 200
}
Enter fullscreen mode Exit fullscreen mode

For more interactive output of the API Documentation, use the URL (https://e5w1sl.deta.dev/docs) which is provided by Swagger UI.

And as Francesco says, NOT DEPLOY on FRIDAY!!!

Conclusion

With the API deployed, this series comes to an end🎉🎉✨✨.

In case you have any doubts or suggestions regarding the article(s), feel free to contact me on my email aunicorndeveloper@gmail.com or on Twitter at @aUnicornDev.

The API built in this series is used in the tabsMonster project. Everyone is more than welcome to checkout and contribute in this Open Source Project.

GitHub logo aUnicornDev / tabsMonster

A Chrome extension to keep a tab on your Browser Tabs

Important Links

Below are the links for docs and products if you are more interesed.

Thank you for Reading!!

Why are you still here?

Top comments (0)