Table of Contents
Features
- Submit contact forms with the following fields:
- Name
- Phone
- Title
- Message
- Send email notifications to the recipient using an SMTP server
- Send Discord notifications to a Discord webhook
- Store contact form submissions in a PostgreSQL database
- API documentation using Swagger UI and ReDoc
Requirements
- Python 3.8+
The API is built using the following libraries:
- FastAPI - Web framework
- Pydantic - Settings management using Pydantic
- SQLAlchemy - Database ORM
- PostgreSQL - Database
- Psycopg - psycopg2 - Python-PostgreSQL Database Adapter
- Python-Multipart - A streaming multipart parser for Python.
- smtp - Email sending
- uvicorn - ASGI server
- python-dotenv - Environment variables
- httpx - HTTP client
- Discord Webhooks - Discord Webhooks
Installation
Running Locally
To run the API locally, follow these steps:
Note:
- Make sure you have Python 3.8+ installed on your system.
-
You need to have a PostgreSQL database running on your system and create a database for the API. Refer to the PostgreSQL documentation for more information.
-
Clone the repository:
git clone https://github.com/nguyenhongthe/contact_form_api.git
cd contact_form_api
- Install the required dependencies:
pip install -r requirements.txt
-
Create a
.env
file in the root directory and set the necessary environment variables. Refer to the.env.example
file for guidance. -
Run the API server:
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
The API should now be up and running at http://localhost:8000.
Usage
API documentation can be accessed through the following URLs:
- Swagger UI - http://localhost:8000/api/docs
- ReDoc - http://localhost:8000/api/redoc
- OpenAPI JSON - http://localhost:8000/api/openapi.json
Submitting a Contact Form
To submit a contact form, send a POST request to the /submit-contact-form
endpoint with the following form parameters:
name
: Name of the senderemail
: Email of the senderphone
: Phone number of the sendertitle
: Title of the messagemessage
: Message content
Example using cURL:
curl -X 'POST' \
'http://localhost:8000/submit-contact-form' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'name=My%20Name&email=myname%40gmail.com&phone=0123456789&title=Hello&message=Hello%20World'
Configuration
The application uses environment variables for configuration. Make sure to define these variables in the .env
file before running the application. Refer to the .env.example
file for a list of required variables.
For detailed configuration options, consult the Settings
class in the main.py
file.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request if you find a bug or want to improve the API.
License
This project is licensed under the MIT License. See LICENSE for more information.