This is an example of a Python FastAPI Web Service that interacts with Oracle Database.
- Python 3
- Oracle Database connection details
- curl (optional)
git clone https://github.com/oracle-quickstart/fastapi-oracle-database-example.git
cd fastapi-oracle-database-example
python3 -m pip install -r requirements.txt
Set database username and connection string environment variables:
PYTHON_USERNAME
PYTHON_CONNECTSTRING
export PYTHON_USERNAME=cj
export PYTHON_CONNECTSTRING=localhost/orclpdb1
export PYTHON_USERNAME=admin
export PYTHON_CONNECTSTRING='(description= (retry_count=15)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.ap-sydney-1.oraclecloud.com))(connect_data=(service_name=gxxxxxx_yxxxx_high.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))'
To run the FastAPI app, use the following command:
uvicorn fapi:app --reload
If the uvicorn binary is not in your PATH, you may need to specify the full path, for example:
$HOME/Library/Python/3.9/bin/uvicorn fapi:app --reload
Enter your database user password when prompted.
The app will be accessible at http://localhost:8000 or http://YOUR_LOCAL_IP:8000
Endpoints are shown at http://localhost:8000/docs
They are:
POST
: to create dataGET
: to read dataPUT
: to update dataDELETE
: to delete data
Alternative documentation is at http://localhost:8000/redoc
To use the FastAPI app, you can use the 'Try it out' buttons on
http://localhost:8000/docs or alternatively use a command line tool such as
curl
.
curl -X 'POST' \
'http://localhost:8000/orders/' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"order_id": 1,
"product_name": "New Product",
"quantity": 10
}'
curl -X 'PUT' \
'http://localhost:8000/orders/1' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"order_id": 1,
"product_name": "Updated Product",
"quantity": 20
}'
curl -X 'GET' \
'http://localhost:8000/orders/1' \
-H 'accept: application/json'
curl -X 'DELETE' \
'http://localhost:8000/orders/1' \
-H 'accept: application/json'
With FastAPI you get OpenAPI docs generated automatically if you go to path /docs
http://127.0.0.1:8000/docs

http://127.0.0.1:8000/redoc

Oracle Database also provides native REST APIs capabilities. To learn more, see :
Product Page : https://www.oracle.com/database/technologies/appdev/rest.html
Hands-on LiveLab : https://apexapps.oracle.com/pls/apex/r/dbpm/livelabs/view-workshop?wid=815
This project welcomes contributions from the community. Before submitting a pull request, please review our contribution guide
Please consult the security guide for our responsible security vulnerability disclosure process
Copyright (c) 2023 Oracle and/or its affiliates.
Released under the Apache License version 2.0 as shown at http://www.apache.org/licenses/.