Skip to main content

How do I implement the Reality Defender API onto my backend or service?

Diana Hsieh avatar
Written by Diana Hsieh
Updated over 11 months ago

You can find the full Reality Defender API documentation here.

If you are leveraging Python, here is an example script that will allow you to upload files to Reality Defender. Make sure to replace the API Key with your own personal key.

import requests
import os
import sys

# This function gets a pre-signed url that you can post a file to

def get_signed_url(filename, token):
url = "https://api.prd.realitydefender.xyz/api/files/aws-presigned"
filesize=os.path.getsize(filename)
payload = {
"fileName":filename,
"fileSize":filesize,
}
headers = {"x-api-key":token,"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
return response.json()

# This function uploads the file to the pre-signed url you received in the prior function
def upload_file(filename, token, signed_url):
with open(filename, 'rb') as file:
file_data = file.read()

try:
response = requests.put(signed_url, data=file_data)
response.raise_for_status()
print('File upload successful!' + filename)
return response
except requests.exceptions.RequestException as e:
print(f'File upload failed with error: {e}')


# Enter your API token here
token = "ENTER TOKEN HERE"

# Accept file path from command line
if len(sys.argv) != 2:
print("Usage: python sample_post_script.py <file_path_or_directory_path>")
sys.exit(1)

path = sys.argv[1]
if os.path.isfile(path):
signed_url = get_signed_url(path,token)["response"]["signedUrl"]
upload_file(path,token,signed_url)
elif os.path.isdir(path):
for root, dirs, files in os.walk(path):
for file in files:
file_path = os.path.join(root, file)
signed_url = get_signed_url(file_path,token)["response"]["signedUrl"]
upload_file(file_path,token,signed_url)
else:
print(f"Error: {path} is not a valid file or directory.")
sys.exit(1)

If you are leveraging Python, here is an example script that will allow you to get files from Reality Defender that have finished analyzing. Make sure to replace the API Key with your own personal key.

import requests
import sys

def get_media_detail(request_id, token):
url = "https://api.prd.realitydefender.xyz/api/media/users/"+request_id
headers = {"x-api-key":token,"Content-Type": "application/json"}
response = requests.get(url, headers=headers)
return response.json()

token = "ENTER TOKEN HERE"

if len(sys.argv) > 2:
print("Usage: python sample_get_script.py <request_id_or_empty_to_get_all_media>")
sys.exit(1)
elif len(sys.argv) == 2:
request_id = sys.argv[1]
print(get_media_detail(request_id,token))
else:
request_id=""
print(get_media_detail(request_id,token))

Did this answer your question?