Creating APIs in Python using FastAPI with a complimentary Oracle Database and deploying it for no cost on Oracle Cloud.

Posted by

Develop APIs in Python with FastAPI and a Free Oracle Database

Develop APIs in Python with FastAPI and a Free Oracle Database

FastAPI is a modern web framework for building APIs with Python. It is fast, easy to use, and comes with automatic validation and interactive documentation. In this article, we will show you how to develop APIs using FastAPI and a free Oracle database, and deploy it for free on Oracle Cloud.

Setting up FastAPI and Oracle Database

To get started, you will need to install FastAPI and Oracle Instant Client on your system. You can do this by following the instructions in the FastAPI documentation and Oracle’s website. Once you have everything set up, you can start writing your API code in Python using FastAPI’s syntax.

Next, you will need to set up your Oracle database. You can sign up for a free Oracle database account on Oracle Cloud and create a new database instance. Make sure to note down your database credentials as you will need them to connect to your database from your FastAPI code.

Developing APIs with FastAPI

Now that you have your development environment set up, you can start writing your API code in Python using FastAPI. You can define your API routes, request parameters, and response models using FastAPI’s decorators. Here is an example of a simple API endpoint that fetches data from your Oracle database:

“`python
from fastapi import FastAPI
import cx_Oracle

app = FastAPI()

dsn = cx_Oracle.makedsn(”, ”, service_name=”)
connection = cx_Oracle.connect(”, ”, dsn)

@app.get(“/data”)
async def get_data():
cursor = connection.cursor()
cursor.execute(“SELECT * FROM data_table”)
data = cursor.fetchall()
cursor.close()
return {“data”: data}
“`

Deploying your API on Oracle Cloud

Once you have developed your API, you can deploy it on Oracle Cloud for free. You can do this by following these steps:

  1. Log in to your Oracle Cloud account
  2. Click on ‘Create a VM instance’ and select a compute shape
  3. Upload your FastAPI code to your VM instance
  4. Install FastAPI dependencies and Oracle Instant Client on your VM instance
  5. Run your FastAPI code on your VM instance
  6. Access your API using your VM instance’s public IP address

By following these steps, you can deploy your API on Oracle Cloud and start using it for free. You can also scale your API by upgrading your VM instance or adding more resources to it. Happy coding!

0 0 votes
Article Rating
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@mcadilgmail
7 months ago

This video is a goldmine. Even better with all those errors. Exactly what I needed for my project. Thank you.

@MrKoziKozu
7 months ago

Hi, I have a problem with connecting to the DB with Python code. Everything works fine with sqldeveloper and testing in code with oracledb.conncet() but when I'm trying to connect with sqlalchemy "create_engine()" I get "DPY-4027: no configuration directory to search for tnsnames.ora". Could someone help me with that? I took a look in the documentation and didn't find a reason. I also can't find anywhere a way to connect to the db with TLS connection string like it is done in the tutorial 🙁

@CodeMindsBusinessBytes
7 months ago

Timecodes

0:00 – Intro
2:49 – Fist steps: Downloading and Installing Software
4:32 – Fist steps: Creating a virtual environment
8:10 – Fist steps: Out first API
9:32 – Fist steps: More APIs
10:56 – Fist steps: Automatic documentation
12:40 – Fist steps: Quick recap
13:06 – Fist steps: Path variables
15:19 – Fist steps: Order matters
17:09 – Fist steps: Query parameters
20:36 – Fist steps: Request body
24:57 – Connect to Oracle DB: Why Oracle
27:02 – Connect to Oracle DB: Creating an account
28:45 – Connect to Oracle DB: Account creation continued
29:08 – Connect to Oracle DB: Account creation continued 2
29:32 – Connect to Oracle DB: Creating autonomous database
32:34 – Connect to Oracle DB: Create connections in SQL Developer
35:56 – Connect to Oracle DB: Test Oracle connection in Python
38:53 – Connect to Oracle DB: Database connections and database models
54:55 – Connect to Oracle DB: Fetch data from database in a path operation
58:28 – Create TODOs: List TODOs and create TODOs
1:08:45 – Create TODOs: Delete a TODO
1:13:09 – Create TODOs: Update a TODO
1:16:55 – Create Users: Create users table
1:18:11 – Create Users: Create routes and users
1:26:57 – Create Users: Hashing the password
1:30:35 – Create Users: Showing docs
1:30:54 – Authentication: Login endpoint
1:42:03 – Authentication: Generate access token
1:50:08 – Authentication: Create access token
1:52:24 – Authentication: Require API endpoints an access token
1:56:32 – Authentication: Get current username
2:02:58 – Authentication: Make every route require get_current_user
2:04:02 – TODOs with username: create foreign key
2:08:48 – TODOs with username: apply changes to all endpoints
2:13:14 – TODOs with username: continue editing the APIs
2:15:55 – Alembic: install alembic
2:17:14 – Alembic: configure alembic
2:19:17 – Alembic: making changes to the database
2:20:45 – Alembic: autogenerate revision
2:34:00 – CORS: adding CORS
2:26:05 – Containerise: create Dockerfile
2:39:43 – Containerise: download Rancher Desktop
2:40:35 – Containerise: building an image
2:42:04 – Containerise: Publish to GitHub
2:43:14 – Containerise: Git reset
2:43:56 – Containerise: Git config
2:45:11 – Deploy to OCI container instance: Introduction
2:46:41 – Deploy to OCI container instance: Creating VCN, Subnets, NAT GW and Instance
2:55:36 – Deploy to OCI container instance: Git clone and install docker
2:56:44 – Deploy to OCI container instance: Build image and run docker
2:57:26 – Deploy to OCI container instance: Docker run and create OCI Registry repository
2:58:37 – Deploy to OCI container instance: Docker push, and create policies
3:07:23 – Deploy to OCI container instance: Add dynamic group and policies
3:09:40 – Deploy to OCI container instance: Create container instance
3:19:30 – Deploy to OCI container instance: Create a Load Balancer
3:28:32 – Deploy to OCI container instance: Add SSL HTTPS TLS certificates
3:35:46 – Deploy to OCI private instance: Running unicorn
3:40:11 – Deploy to OCI private instance: Setting Load Balancer with vm instance
3:44:09 – Deploy to OCI private instance: Make a service to start on boot
3:55:06 – Deploy to OCI public instance: preparation and introduction
3:56:58 – Deploy to OCI public instance: creating an image
3:59:28 – Deploy to OCI public instance: edit DNS on Cloudflare
4:00:30 – Deploy to OCI public instance: create instance and load nginx
4:05:27: Deploy to OCI public instance: using nginx to proxy port 8000
4:06:42 – Deploy to OCI public instance: solution to error
4:07:11 – Deploy to OCI public instance: creating SSL certificates with certbot
4:09:14 – Deploy to OCI public instance: finishing installation
4:13:22 – Goodbye