# How to host FastAPI application on Deta Space

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 align="left")

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] 

# **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).

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

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

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

# 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

```bash
space login
```

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

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

## 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.

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

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

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

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

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] 

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.

```bash
test
docs
```

For our project, we will be ignoring the folders

```bash
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.

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

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

```bash
space new
```

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

## 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.

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

```yaml
# 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 (1023×369) (](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.  

```bash
space push
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679943610440/dccfcc03-b91e-4a3c-837d-501b6220f580.gif align="center")

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).

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

# 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] 

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!
