How to upload multiple images to Telegraph using Python API

I’m working on a Python project where I need to upload multiple images to Telegraph. I have a bunch of photos stored locally and want to create a Telegraph page that displays all of them.

I found the create_page() method which has parameters like title, content, html_content, author_name, etc. My question is whether I can pass image URLs or file paths directly to the content parameter when creating a page.

Here’s what I’m trying to achieve:

def upload_images_to_telegraph(image_list, page_title):
    # image_list contains paths to local images
    # Need to upload them and create a page
    
    result = telegraph_api.create_page(
        title=page_title,
        content=???,  # How to include multiple images here?
        author_name="My Bot"
    )
    return result

I’m still learning Python so any detailed explanation would be really helpful. Thanks in advance!

Telegraph API has a two-step process that tripped me up at first. You can’t just throw local file paths into the content parameter - Telegraph needs the images on their servers first. Here’s what actually works: upload each image separately using upload_file(), which gives you back a URL path. Then build your content as a list of dictionaries where each image becomes a tag element with src pointing to that uploaded path. The content parameter wants a specific format - usually a list with dictionaries containing tag and attrs keys. For images, you’d use tag: ‘img’ and attrs with the src from your upload step. Took me several tries to nail this since the docs aren’t super clear about the exact structure needed.

Had this exact problem building my image gallery bot last month. Telegraph makes you upload files one by one first, then build the content array correctly. Upload each image with their upload endpoint - you’ll get back a path like ‘/file/abc123.jpg’. For the content parameter, format it as an array where each image looks like {‘tag’: ‘img’, ‘attrs’: {‘src’: ‘https://telegra.ph/file/abc123.jpg’}}. Here’s the gotcha - you need the full Telegraph URL, not just the path they give you. Also, Telegraph has file size limits so check your images aren’t too big or you’ll get weird errors.

u gotta first upload each pic with the upload method, then use the returned URLs in your content. telegraph won’t take local paths - the pics need to be hosted on their servers before you can create that page.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.