[Python] Uploaded image from script to Figma file through API on Python doesn't appear

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to upload several images from my pc to figma file. The script sees the file, its' nodes, ids, but can't put the image inside file, despite no errors. I've created server through python -m http.server 8000, and uploading from there. If anyone has ideas how to fix this, please, write or ask details.

    Here's code:

    import requests
    import os
    import traceback

    # Personal Figma token
    TOKEN = "token"
    FILE_KEY = "file key"

    # Auth
    HEADERS = {
    "X-Figma-Token": TOKEN
    }

    # Uploading to Figma
    def upload_image_to_node(node_id, image_url):
    url = f"https://api.figma.com/v1/images/{FILE_KEY}"
    json_data = {
    "requests": [
    {
    "node_id": node_id,
    "fill": {
    "type": "IMAGE",
    "image_url": image_url,
    "scale_mod": "FILL"
    }
    }
    ]
    }
    response = requests.post(url, headers=HEADERS, json=json_data)
    if response.status_code == 200:
    print(f"Image {image_url} successfully uploaded to node {node_id}.")
    else:
    print(f"Error: {response.status_code}, {response.text}")

    # Main
    def process_images(input_folder, node_id):
    base_url = "http://localhost:8000"
    for filename in os.listdir(input_folder):
    if filename.endswith((".png", ".jpg")):
    image_url = f"{base_url}/{filename}"
    upload_image_to_node(node_id, image_url)

    try:
    node_id = "0:1" # Main node in Figma
    process_images("out/", node_id) # out/ is where images stored
    except Exception as e:
    print("Error occured:")
    traceback.print_exc()
    input("Press Enter to quit..")

    Continue reading...

Compartilhe esta Página