How to host FastAPI application on Deta Space
Tags: python, programming, fastapi
Published: 2023-03-29 02:24:15

---
title: How to host FastAPI application on Deta Space
published: true
description: Have you heard of Deta Space, if you haven't than this blog is showing how to host FastAPI app on Deta. Can it be better cloud for free app hosting?
date: 2023-03-28 02:30:39 UTC
tags: #python #programming #FastAPI
canonical_url: https://blog.akashrchandran.in/how-to-host-fastapi-application-on-deta-space
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f55b2nx5vya3rrl0wym4.png
---

Almost 2 years ago while browsing through the FastAPI docs, I came across their sponsors one of which was [deta.sh](https://deta.sh) and it is still there you can view it at [FastAPI Docs](https://fastapi.tiangolo.com/#sponsors). As a curious person, I opened the link and browsed through their website. At that time they had a minimalist design.

![old sample view of deta.sh website](https://resource.fyi/_next/image?url=http%3A%2F%2Fres.cloudinary.com%2Fdnr4p6zlc%2Fimage%2Fupload%2Fv1666362295%2Fraimcg6gvhi7ydemcfb2.png&w=3840&q=75)

They didn't have a GUI to manage apps only CLI to deploy and manipulate apps, the only thing they provided was the register/login and docs if I remember correctly. I tried deploying some apps but failed for some I don't remember. So let the deta cloud and tried something else.

But yesterday I came across the same site and was amazed that they are shifting to a new better version called [Deta Space](https://deta.space/). This is in still beta but it has everything a modern platform has like GUI to manage apps, view logs, better docs and even a page to find user-build apps that are deployed on Deta Space. So I decided to test it out by deploying a sample FastAPI application. We will use the same repository as I used in my old blog showing the deployment of the FastAPI application on Render.

[https://blog.akashrchandran.in/deploying-fastapi-application-to-render](https://blog.akashrchandran.in/deploying-fastapi-application-to-render)

## **Creating** [**Deta Space**](https://deta.space/) **Account**

As with any other site, you will need an account to host apps on [Deta Space](https://deta.space/). They don't provide any third-party signup method so you will have to have to use a good old email password. You are required to choose a unique username, I think it's for subdomain assignment not sure yet though. You can sign up using this [link](https://deta.space/signup).

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679927384583/500d1f86-fc05-4761-92f0-77ef17d23df4.png)

After login, you should be redirected to the dashboard and you should look something like this

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679928791396/143ca38c-9705-447c-8e47-0f5734d9edb9.png)

## Setting Up CLI

Although the site has more GUI-rich features it still lacks a deployment option. So we have to set up our CLI tool if we want to continue. [Setting up the CLI - Space Docs (](https://deta.space/docs/en/basics/cli)deta.space[)](https://deta.space/docs/en/basics/cli) here you can find the instructions to do so. After installation login can be done by using the command

```
space login

```

If installed correctly it should show something like this. Next, we need is the authentication token from Deta space.

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679936113776/cde33a31-7579-4afb-9172-4ff4a81d9bc0.png)

### Getting token from Deta Space

After you log in you will be redirected to the dashboard, use the command palette. Just search for the settings and then click generate a new token.

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679936640983/cbad93e1-1d2f-49a7-a212-e180aca37879.png)

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679936700259/99f1a980-b930-4a7b-b9f1-24308c95df83.png)

After that copy the token and come back to the terminal page. And paste it onto the terminal

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679937233478/3ba11078-f563-4576-a96e-25a7e7f794ed.png)

we have successfully set up CLI. Now let's move on to the next part which is pushing the code to Deta Space.

I am not going to show to how to code or install FastAPI for that you can read my old blog post.

[https://blog.akashrchandran.in/deploying-fastapi-application-to-render#heading-lets-make-a-simple-api-using-fastapi](https://blog.akashrchandran.in/deploying-fastapi-application-to-render#heading-lets-make-a-simple-api-using-fastapi)

You can also view the code project at my GitHub repository at [akashrchandran/simple-api (](https://github.com/akashrchandran/simple-api)[github.com](http://github.com)[)](https://github.com/akashrchandran/simple-api).

## Creating .spaceignore file

To exclude certain files and directories from being uploaded during space push, use the .spaceignore file. It acts similarly to a .gitignore file, where each line in the file specifies a pattern. All files and directories matching the patterns will be excluded from the push.  
For example, a `.spaceignore` file with the following content `space push` will ignore the `test` and `docs` paths.

```
test
docs

```

For our project, we will be ignoring the folders

```
venv
__pycache__

```

## **Deploying to Deta Space**

After all the hard work we have done here is going to pay off, let's make the app accessible from anywhere on the planet. Open up the code in visual studio code or any other IDE or Notepad your wish. And open the terminal on the side or use the integrated terminal option in VS code.

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679940982983/7d575395-2e0d-4c04-a848-2164bec1fef4.png)

Make sure you have the Space CLI installed & authenticated, then run the following command to create a new Space project in the current directory.

```
space new

```

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679941199101/c86860d0-967b-4e58-9d7e-5740eceea9da.png)

### Editing the Spacefile

This will create a `Spacefile` in the root directory. And it will only have the version `v:0` as text and comment to the docs. This is where we define our runtime engine and other requirements. To get a clear idea about it you can visit the [Spacefile - Space Docs (](https://deta.space/docs/en/reference/spacefile/)[deta.space](http://deta.space)[)](https://deta.space/docs/en/reference/spacefile/). To run the FastAPI app we need to define some requirements in the Spacefile. Here's what the file looks like after adding the required lines.

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679941690567/ea9cafee-b456-4015-af20-b0101492e191.png)

```
## Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
icon: ./icon.png
app_name: "Simple FastAPI APP"
micros:
  - name: simple-fastapi
    src: ./
    engine: python3.9
    dev: .venv/bin/uvicorn main:app --reload

```

I ran into a few troubles while making the Spacefile, it seems python apps don't support `run` commands. Use `dev` to specify a command which starts your Micro in **development** mode. This command will be used to start your micro when you run.  
You can specify the version of python you need using the key `engine`. There are only two versions available currently `python3.9` and `python3.8`.  
`src` is the directory where the main.py file resides. You can keep it inside another folder and then specify the path to that folder.

for the `icon` I am using the logo of FastAPI, you can check it out at [logo-teal.png (1023369) (](https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png)[tiangolo.com](http://tiangolo.com)[)](https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png).

**PS:** You are required to keep the file main.py as the entry file or it will not work. Make sure you use `main.py` for any python project.

### Deploying the app

To deploy we just need to give the command, and it should automatically deploy and give the output URL.

```
space push

```

![gif](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hkcesl2j4i81nnazh9di.gif)

Congratulations the app is live🥳🥳. And you will be able to see the application in your dashboard. You can find my deployed version at [https://simplefastapi-1-h6036723.deta.app](https://simplefastapi-1-h6036723.deta.app).

![image](https://cdn.hashnode.com/res/hashnode/image/upload/v1679943882001/786030ad-54a9-49a0-8b72-7da06d2f5b0a.png)

## Conclusion

This was a simple blog explaining the way to deploy the FastAPI application on Deta Space. There are many more things you can add to the project, I am just setting an example. The documentation is pretty amazing and you can find almost all the parts documented, do give it a read at [Getting started - Space Docs (](https://deta.space/docs/en/introduction/start)[deta.space](http://deta.space)[)](https://deta.space/docs/en/introduction/start).

Overall I would say it's pretty easy compared to other services, even though some services are popular yet it's hard to get the app deployed on the service. The Deta Space is not that famous but it was easy to deploy apps. I am sure that I will use the service again to host my apps.

The Deta Space is still under construction and it's developed and updated daily. This makes it vulnerable to bugs and other problems. If you are stuck at some place, first read the docs. If you're still stuck after that then you can join their Discord server and post your queries and problems.

[https://discord.com/invite/K3gYNqHXUU](https://discord.com/invite/K3gYNqHXUU)

The community may be small but they are active and almost answers every single query raised. Giving feedback will help build the platform faster and better, do share your review on Discord.

Finally thank you for reading this blog, it was a good experience to share my view and knowledge about the platform. Have a great day!