An open-source, unofficial REST API for accessing drug information from Thailand's National List of Essential Medicines (NLEM). Built with Rust, Axum, and SQLx for performance, safety, and reliability.
The goal of this project is to provide a clean, accessible, and well-documented API for developers, researchers, and healthcare professionals who need programmatic access to Thailand's essential medicine data.
- Fast & Efficient: Built on the high-performance Axum web framework in Rust.
- Type-Safe: Leverages SQLx for compile-time checked SQL queries.
- Search Functionality: Powerful search endpoint for finding drugs by generic or synonym names.
- Structured Data: Provides drug information, including dosage forms, ED level, warnings, and conditions in a clean JSON format.
- Open Source: Free to use, modify, and contribute to under the MIT License.
Follow these instructions to get a local copy of the project up and running for development and testing purposes.
- Rust Toolchain: Install from rustup.rs.
- PostgreSQL: A running PostgreSQL instance. We recommend using Docker.
sqlx-cli: For database migrations. Install withcargo install sqlx-cli.- Source Data: The project requires the
nlem_2567.csvdata file. (Please provide source/link if available).
-
Clone the repository:
git clone https://github.com/your-username/thai-nlem-api.git cd thai-nlem-api -
Setup the Database:
- If using Docker, start a PostgreSQL container:
docker run --name nlem-db -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypassword -e POSTGRES_DB=nlem_api_db -p 5432:5432 -d postgres:16-alpine
- Create a
.envfile from the example. Copy the content of.env.example(or create it manually).cp .env.example .env
- Update your
.envfile with your database connection string:DATABASE_URL="postgres://myuser:mypassword@localhost:5432/nlem_api_db"
- If using Docker, start a PostgreSQL container:
-
Run Database Migrations: This command will create the necessary tables (
drugs,drug_categories) in your database.sqlx database create # Only needed the first time sqlx migrate run -
Seed the Database: Place the
nlem_2567.csvfile inside thedata/directory. Then, run the seeder to populate the database. This might take a few minutes.cargo run -- seed
-
Run the API Server: Once seeding is complete, you can start the API server.
cargo run
The server will be running at
http://localhost:3000.
The API provides several endpoints to access the drug data.
- Endpoint:
GET / - Description: Checks if the API server is running.
- Response:
{ "status": "OK" }
- Endpoint:
GET /api/drugs/search - Query Parameter:
q(string, required) - The search term. - Description: Searches for drugs where the generic name or synonym name matches the query (case-insensitive).
- Example:
GET /api/drugs/search?q=paracetamol - Response: An array of drug objects.
[ { "id": 1, "category_id": 123, "generic_name": "Paracetamol", "syn_name": "Acetaminophen", "detail": null, "drug_type": null, "dosage_forms": ["tab", "syr"], "ed_level": "ก", "recommendations": "แนะนำให้ใช้เป็น first-line drug สำหรับ osteoarthritis", "conditions": null, "warnings": null, "notes": null, "footnote": null, "source_code": "S" } ]
- Endpoint:
GET /api/drugs/:id - Path Parameter:
id(integer, required) - The unique ID of the drug. - Description: Retrieves a single drug by its ID.
- Example:
GET /api/drugs/1 - Response: A single drug object or a
404 Not Founderror.
Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Please make sure to update tests as appropriate and ensure the code is formatted with cargo fmt.
Distributed under the MIT License. See LICENSE file for more information.