Python IPFS HTTP Client Library

0.7.0 · active · verified Thu Apr 16

The ipfshttpclient library provides a client interface to the IPFS HTTP API, allowing Python applications to interact with an IPFS daemon. This library version `0.7.0`, released in March 2021, is tested against go-ipfs v0.7.0 and strives to support the last 5 releases of go-IPFS. It was previously known as `ipfsapi`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a local IPFS daemon, add a file, and retrieve its content by hash. Ensure you have an IPFS daemon running in the background before executing.

import ipfshttpclient
import os

# Ensure an IPFS daemon is running (e.g., `ipfs daemon` in your terminal)
# Connect to the local IPFS daemon (default: /dns/localhost/tcp/5001/http)
try:
    client = ipfshttpclient.connect()
    print("Successfully connected to IPFS daemon.")
    
    # Example: Add a simple text file
    file_content = "Hello, IPFS!\n"
    file_name = "test.txt"
    with open(file_name, 'w') as f:
        f.write(file_content)
    
    res = client.add(file_name)
    print(f"Added '{file_name}': {res}")
    
    # Example: Cat (retrieve content) by hash
    retrieved_content = client.cat(res['Hash']).decode('utf-8')
    print(f"Retrieved content for {res['Hash']}:\n{retrieved_content}")
    
    os.remove(file_name)
    
except ipfshttpclient.exceptions.ConnectionError as e:
    print(f"Error connecting to IPFS daemon: {e}")
    print("Please ensure your IPFS daemon is running and accessible (e.g., 'ipfs daemon' in a separate terminal).")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →