diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..074ecd7 --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +# REDCap API credentials: +REDCAP_API_URL=https://redcap.example.edu/api/ +REDCAP_API_KEY=replace-with-your-api-token diff --git a/README.md b/README.md index 5a1ded8..cb95f2a 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,25 @@ To install the bleeding edge version from the github repo, use the following $ pip install -e git+https://github.com/redcap-tools/PyCap.git#egg=PyCap ``` +## Quickstart + +1. Copy `.env.example` to `.env` and update the values with your REDCap endpoint and API token. +2. Install [`python-dotenv`](https://pypi.org/project/python-dotenv/) with `pip install python-dotenv` (or add it to your Poetry environment). + +Then load your credentials and connect: + +```python +from dotenv import load_dotenv +import os +from redcap import Project + +load_dotenv() # reads .env from the project root + +api_url = os.environ["REDCAP_API_URL"] +api_key = os.environ["REDCAP_API_KEY"] +project = Project(api_url, api_key) +``` + ## Documentation Canonical documentation and usage examples can be found [here](https://redcap-tools.github.io/PyCap/). diff --git a/docs/index.md b/docs/index.md index ab5945e..5324f3f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -16,6 +16,13 @@ If you want to load REDCap data into [`pandas`](https://pandas.pydata.org/) data $ pip install PyCap[all] ``` +For secure credential management, install [`python-dotenv`](https://pypi.org/project/python-dotenv/) (`pip install python-dotenv`) and create a `.env` file in your project root with your REDCap credentials: + +```dotenv +REDCAP_API_URL=https://redcap.example.edu/api/ +REDCAP_API_KEY=YourSuperSecretAPIKeyHere +``` + To install the bleeding edge version from the github repo, use the following ```sh diff --git a/docs/quickstart.md b/docs/quickstart.md index ab8f79b..64a132f 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -2,11 +2,24 @@ PyCap makes it very simple to interact with the data stored in your REDCap projects +> First, install python-dotenv (`pip install python-dotenv` or `poetry add python-dotenv`) and create a `.env` file in your project root with your REDCap credentials: + +```dotenv +REDCAP_API_URL=https://redcap.example.edu/api/ +REDCAP_API_KEY=SomeSuperSecretAPIKeyThatNobodyElseShouldHave +``` + +Then update your code to load the values from the environment: + ```python +from dotenv import load_dotenv +import os from redcap import Project -api_url = 'https://redcap.example.edu/api/' -api_key = 'SomeSuperSecretAPIKeyThatNobodyElseShouldHave' +load_dotenv() # Load variables from .env file + +api_url = os.getenv('REDCAP_API_URL') +api_key = os.getenv('REDCAP_API_KEY') project = Project(api_url, api_key) ```