Looking to create an API to automate backup data

Hi, yes, that is possible through the Shopify API. In your Shopify account, go to APPs and create a DEVELOPER APP, granting it access to everything. Install Python on your system, including the modules to be imported in the code above. Here is some code that backs up the data, including the current live theme. The code creates a new and separate directory for each backup. Please note that the Shopify API can be sensitive if too many requests are sent, so I’ve added a 1-second delay between each request. The code is a quick guide, and you may need to customize it to fit your needs. I hope this helps you. Best regards, Stefan.

import requests
import json
import os
from datetime import datetime
import time  # 

# Shopify Store Informationen
API_KEY = 'YOUR API KEY'
PASSWORD = 'YOUR API PASSWORD'
SHOP_NAME = 'YOUR SHOPNAME (e.g. if your internal shopname is example.myshopify.com just enter example )'

# Create Backup Folder
backup_dir = datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
os.makedirs(backup_dir, exist_ok=True)

def save_data_to_file(data, filename):
    with open(os.path.join(backup_dir, filename), 'w') as f:
        json.dump(data, f, indent=4)

# Download
def download_and_save_file(url, filepath):
    response = requests.get(url, auth=(API_KEY, PASSWORD))
    if response.status_code == 200:
        # Create subfolders
        os.makedirs(os.path.dirname(filepath), exist_ok=True)
        
        with open(filepath, 'wb') as f:
            f.write(response.content)
        print(f'File {filepath} saved successfully.')
    else:
        print(f'Error downloading file from {url}: {response.status_code} - {response.text}')

# Shopify API Endpoints
endpoints = [
    'customers.json',
    'orders.json',
    'products.json',
    'pages.json',
    'custom_collections.json',
    'blogs.json',
    'themes.json',
    'smart_collections.json',
    'custom_collections.json',
]

for endpoint in endpoints:
    url = f'https://{SHOP_NAME}.myshopify.com/admin/api/2022-01/{endpoint}'
    response = requests.get(url, auth=(API_KEY, PASSWORD))

    if response.status_code == 200:
        data = response.json()
        filename = endpoint.replace('.json', '.json')
        save_data_to_file(data, filename)
        print(f'Data from {endpoint} saved successfully in {backup_dir}.')
    else:
        print(f'Error fetching data from {endpoint}: {response.status_code} - {response.text}')

# Download LIVE-Theme
themes_endpoint = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes.json'
themes_response = requests.get(themes_endpoint, auth=(API_KEY, PASSWORD))

if themes_response.status_code == 200:
    themes_data = themes_response.json()
    live_theme_id = None

    for theme in themes_data['themes']:
        if theme['role'] == 'main':  
            live_theme_id = theme['id']
            break

    if live_theme_id is not None:
        theme_dir = os.path.join(backup_dir, f'theme_{live_theme_id}')
        os.makedirs(theme_dir, exist_ok=True)

       
        theme_files_endpoint = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes/{live_theme_id}/assets.json'
        theme_files_response = requests.get(theme_files_endpoint, auth=(API_KEY, PASSWORD))

        if theme_files_response.status_code == 200:
            theme_files_data = theme_files_response.json()
            for asset in theme_files_data['assets']:
                asset_key = asset['key']
                asset_path = os.path.join(theme_dir, asset_key)
                asset_url = f'https://{SHOP_NAME}.myshopify.com/admin/api/2023-10/themes/{live_theme_id}/assets.json?asset[key]={asset_key}'
                download_and_save_file(asset_url, asset_path)
                print(f'File {asset_key} from Live Theme saved successfully in {backup_dir}.')
                time.sleep(1)  
        else:
            print(f'Error fetching files for Live Theme: {theme_files_response.status_code} - {theme_files_response.text}')
    else:
        print('No Live Theme found.')
else:
    print(f'Error fetching themes data: {themes_response.status_code} - {themes_response.text}')

print(f'Backup completed successfully in {backup_dir}.')