Skip to content

Commit ced3685

Browse files
authored
🔒 Integrate python-dotenv for Secure API Credential Management (#304)
1 parent ceda5de commit ced3685

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# REDCap API credentials:
2+
REDCAP_API_URL=https://redcap.example.edu/api/
3+
REDCAP_API_KEY=replace-with-your-api-token

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ To install the bleeding edge version from the github repo, use the following
3131
$ pip install -e git+https://github.com/redcap-tools/PyCap.git#egg=PyCap
3232
```
3333

34+
## Quickstart
35+
36+
1. Copy `.env.example` to `.env` and update the values with your REDCap endpoint and API token.
37+
2. Install [`python-dotenv`](https://pypi.org/project/python-dotenv/) with `pip install python-dotenv` (or add it to your Poetry environment).
38+
39+
Then load your credentials and connect:
40+
41+
```python
42+
from dotenv import load_dotenv
43+
import os
44+
from redcap import Project
45+
46+
load_dotenv() # reads .env from the project root
47+
48+
api_url = os.environ["REDCAP_API_URL"]
49+
api_key = os.environ["REDCAP_API_KEY"]
50+
project = Project(api_url, api_key)
51+
```
52+
3453
## Documentation
3554

3655
Canonical documentation and usage examples can be found [here](https://redcap-tools.github.io/PyCap/).

docs/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ If you want to load REDCap data into [`pandas`](https://pandas.pydata.org/) data
1616
$ pip install PyCap[all]
1717
```
1818

19+
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:
20+
21+
```dotenv
22+
REDCAP_API_URL=https://redcap.example.edu/api/
23+
REDCAP_API_KEY=YourSuperSecretAPIKeyHere
24+
```
25+
1926
To install the bleeding edge version from the github repo, use the following
2027

2128
```sh

docs/quickstart.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,24 @@
22

33
PyCap makes it very simple to interact with the data stored in your REDCap projects
44

5+
> 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:
6+
7+
```dotenv
8+
REDCAP_API_URL=https://redcap.example.edu/api/
9+
REDCAP_API_KEY=SomeSuperSecretAPIKeyThatNobodyElseShouldHave
10+
```
11+
12+
Then update your code to load the values from the environment:
13+
514
```python
15+
from dotenv import load_dotenv
16+
import os
617
from redcap import Project
718

8-
api_url = 'https://redcap.example.edu/api/'
9-
api_key = 'SomeSuperSecretAPIKeyThatNobodyElseShouldHave'
19+
load_dotenv() # Load variables from .env file
20+
21+
api_url = os.getenv('REDCAP_API_URL')
22+
api_key = os.getenv('REDCAP_API_KEY')
1023
project = Project(api_url, api_key)
1124
```
1225

0 commit comments

Comments
 (0)