Upload a file to hubspot with hubspot API

Hello everyone, I’m currently trying to use the hubspot API to manage sending files to our hubspot CRM.
When I run this code I get the error:

<Response [401]>
b’{“status”:“error”,“message”:“This hapikey doesn\'t exist.”,“correlationId”:“30cee56e-xxxx-xxxx-a12b-xxxxxxxx”}’

Here is my code in Python :

import requests
import json

# Include your HubSpot API key in the URL
post_url = 'https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=pat-xxx-xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

# Specify the full path to the file you want to upload
file_path = r'MY LOCAL PATH HERE'

file_options = {
 'access': 'PUBLIC_INDEXABLE',
 'overwrite': False,
 'duplicateValidationStrategy': 'NONE',
 'duplicateValidationScope': 'EXACT_FOLDER'
}

files_data = {
 'file': (file_path, open(file_path, 'rb'), 'application/octet-stream'),
 'options': (None, json.dumps(file_options), 'application/json'),
}

r = requests.post(post_url, files=files_data)

print(r)
print(r.content)

Thank you :slightly_smiling_face: !

Hey, @BJellouli :waving_hand: Welcome to our community! HAPI keys are deprecated and no longer in use. You’ll need to create a Private App (for working with a single portal) or create a Public App and use OAuth if you need to work with multiple portals.

You can adjust your example to use a bearer token in the headers like this:

import requests
import json

# Use Bearer token for authentication
post_url = 'https://api.hubapi.com/filemanager/api/v3/files/upload'
headers = {
 'Authorization': 'Bearer your_bearer_token_here'
}

# Specify the full path to the file you want to upload
file_path = r'MY LOCAL PATH HERE'

file_options = {
 'access': 'PUBLIC_INDEXABLE',
 'overwrite': False,
 'duplicateValidationStrategy': 'NONE',
 'duplicateValidationScope': 'EXACT_FOLDER'
}

files_data = {
 'file': (file_path, open(file_path, 'rb'), 'application/octet-stream'),
 'options': (None, json.dumps(file_options), 'application/json'),
}

r = requests.post(post_url, files=files_data, headers=headers)

print(r)
print(r.content)

I hope that helps get you moving forward.

Have fun building! — Jaycee