Step 5: Upload Instruments¶
This guide explains how to upload rcol instrument templates to your REDCap project using PyCap.
Prerequisites¶
Basic Upload¶
1. Store Your API Key¶
You need your REDCap API key available locally as RC_API_KEY.
Recommended: .env file (works well with the tutorials)
Alternative: Environment variable
Security
Never commit .env files or API keys to version control.
Add this to your .gitignore:
2. Create the Upload Script¶
Create a new Python file:
upload_instruments.py
from dotenv import load_dotenv
import os
import pandas as pd
from redcap import Project, RedcapError
# Import instruments from rcol
from rcol.instruments import fal, ehi, bdi_ii
# Load API key from .env file
load_dotenv()
RC_API_KEY = os.getenv("RC_API_KEY")
# Combine instruments into one DataFrame
instruments = pd.concat([fal, ehi, bdi_ii], ignore_index=True)
print(f"📋 Preparing to upload {len(instruments)} fields...")
# Initialize REDCap project connection
api_url = "https://redcapdev.uol.de/api/"
rc_project = Project(api_url, RC_API_KEY)
# Upload instruments to REDCap
try:
response = rc_project.import_metadata(instruments, import_format="df")
print(f"✅ Successfully uploaded {response} fields to REDCap")
except RedcapError as e:
print(f"❌ Error uploading to REDCap: {e}")
3. Run the Script¶
Expected output:
4. Verify in REDCap¶
- Log in to REDCap
- Open your project
- Go to Online Designer
- You should see your uploaded instruments
Upload Selected Instruments¶
To upload specific instruments:
upload_selected.py
from dotenv import load_dotenv
import os
import pandas as pd
from redcap import Project, RedcapError
from rcol.rtg import (
study_participant_information,
becks_depression_inventory_ii,
sf12,
moca,
)
load_dotenv()
RC_API_KEY = os.getenv("RC_API_KEY")
# Combine selected instruments
instruments = pd.concat([
study_participant_information,
becks_depression_inventory_ii,
sf12,
moca,
], ignore_index=True)
print(f"📋 Uploading {len(instruments)} fields...")
# Upload
api_url = "https://redcapdev.uol.de/api/"
rc_project = Project(api_url, RC_API_KEY)
try:
response = rc_project.import_metadata(instruments, import_format="df")
print(f"✅ Successfully uploaded {response} fields")
except RedcapError as e:
print(f"❌ Error: {e}")
Handling Existing Instruments¶
Important
Uploading instruments overwrites existing metadata. Back up your project first!
Backup Before Upload¶
from redcap import Project
rc_project = Project("https://redcapdev.uol.de/api/", RC_API_KEY)
# Export current metadata as backup
metadata = rc_project.export_metadata(format_type="df")
metadata.to_csv("metadata_backup.csv", index=False)
print(f"✅ Backup saved: {len(metadata)} fields")
Export and Review¶
# View current project structure
metadata = rc_project.export_metadata(format_type="df")
print("Current instruments:")
print(metadata["form_name"].unique())
What's Next?¶
After uploading your instruments, enable them as surveys:
Troubleshooting¶
Error: 'The field_name column contains duplicate values'
- Check that you're not uploading duplicate instruments
- Each field_name must be unique across all instruments
- Use the validation function above to identify duplicates
Error: 'You do not have API Import/Update privileges'
- Contact your REDCap administrator
- Request Import/Update permissions for your API token
Only some fields were uploaded
- Check the response message for the actual count
- Verify field_type values are valid REDCap types
- Look for special characters in field_label or choices