Create a personal REST API to translate Hanzi Chinese characters to Pinyin
This is a simple REST API that translates Hanzi Chinese characters to Pinyin. It uses the following two sources:
- Pinyin Jyutping Python Library
- DeepL Translation API. You need to setup a free account to get the API key
- Clone this repository
- Install the requirements:
pip install -r requirements.txt
- Create a
.env
file in the root directory and add the following variables:
APP_API_KEY_1=<your-api-key>:<user_alias>
APP_API_KEY_2=<your-api-key>:<user_alias>
DEEPL_API_KEY=<your-deepl-api-key>
- replace
<your-api-key>
with a secure random string of your choice. This will be used to authenticate requests to the API. - Note:
<user_alias>
can be any string and can be used as an identifier of who is calling your API when looking at the API logs. e.g.2lkmglk23m:sam
APP_API_KEY_2
and upwards can be used to add additional API keys. You can add as many as you like.- replace
<your-deepl-api-key>
with your created API key from DeepL.
- Run the app:
python main.py
- The app will run by default on
http://127.0.0.1:8000
When you run the app, you can access the API at http://127.0.0.1:8000/
.
You will also need to set the request header acces_token
to the value of the APP_API_KEY
variable you set in the .env
file.
The API has two endpoints:
/
- returns hello world/hanzi/:hanzi-string
- translates a Hanzi string to Pinyin