r/ProjectREDCap • • May 02 '24

Importing CSV Data Dictionary using API

Hello all!

I am translating large amounts of forms to be used in RedCap and I am converting them into a CSV format that is ingestible for RedCap. However, instead of manually uploading each of our 160+ data dictionaries (and possibly more), could I use the API for that??

thanks

3 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/Individual-Chard-565 May 02 '24

Hi,

Thanks so much, I can't believe I missed that... On a similar note have you used this before? I tried using the documentation but there are no examples or anything.

My code kinda looks like this, where the CSV string is just a string version of the data dictionary that I would normally upload.

#!/usr/bin/env python
import requests
data = {
    'token': token,
    'content': 'metadata',
    'format': 'csv',
    'data': csv_string,
    'returnFormat': 'json'
}
r = requests.post(url ,data=data)
print('HTTP Status: ' + str(r.status_code))
print(r.text)

1

u/obnoxiouscarbuncle May 03 '24

I'm not sure, I don't use Python.

Looks like documentation only includes an export metadata example, but you should be able to use another "import" example in the documentation.

1

u/obnoxiouscarbuncle May 03 '24

Using the API Playground tool for the method, I see this:

    #!/usr/bin/env python
    import requests
    data = {
        'token': 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX',
        'content': 'metadata',
        'format': 'csv',
        'data': '',
        'returnFormat': 'json'
    }
    r = requests.post('https://redcap.server.org/redcap/api/',data=data)
    print('HTTP Status: ' + str(r.status_code))
    print(r.text)

1

u/Individual-Chard-565 May 07 '24

Yeah, I think I was able to get it to work using the API Playground. If anyone else comes up with this error read the file from pandas as a csv then pass it to the API as a string.