aioipfs

Asynchronous IPFS client API for Python3’s asyncio.

Installation

pip install aioipfs

If you want to use orjson to decode JSON messages:

pip install 'aioipfs[orjson]'

Async IPFS client

To create an AsyncIPFS client instance it’s recommended to specify the node’s RPC API address with a multiaddr:

import aioipfs

client = aioipfs.AsyncIPFS(maddr='/ip4/127.0.0.1/tcp/5001')

client = aioipfs.AsyncIPFS(maddr='/dns4/localhost/tcp/5001')

The default constructor assumes that the kubo node you want to connect to is localhost on port 5001 (use the host and port keyword arguments to set different values):

client = aioipfs.AsyncIPFS()

client = aioipfs.AsyncIPFS(host='10.0.12.3', port=5003)

Maximum HTTP connections and read timeout parameters:

client = aioipfs.AsyncIPFS(host='localhost', port=5008, conns_max=20)

client = aioipfs.AsyncIPFS(host='localhost', port=5008, read_timeout=30)

Async context manager

You can use an async context manager in your code:

async with aioipfs.AsyncIPFS() as client:
    async for reply in client.ping(peer_id):
        ...

Closing

When finished you should always call AsyncIPFS.close() on your client:

await client.close()

API sectioning

The kubo (formerly called go-ipfs) RPC HTTP API provides many endpoints which allow to access the different subsystems of the IPFS daemon. You can read the RPC API specifications here.

Important: The aioipfs client API closely follows kubo’s RPC endpoints path hierarchy, as each subsystem has its own namespace/attribute inside the AsyncIPFS client object, for example:

etc …

All API functions will raise an APIError (or a more specific exception) if there’s an error raised during the request.

Core API

aioipfs.AsyncIPFS.core

Gives access to the CoreAPI

Adding files

Add files to the IPFS repository with the api.CoreAPI.add() async generator:

async for added in client.core.add('file1.txt', '/usr/local/src',
        recursive=True):
    print(added['Hash'])

async for added in client.core.add(['one', 'two', 'three'],
        wrap_with_directory=True):
    ...

cids = [entry['Hash'] async for entry in client.add(dir_path)]

Entries yielded by the generator are as returned by the IPFS daemon, dictionaries with Name, Hash and Size keys.

Adding bytes

Add some bytes with api.CoreAPI.add_bytes():

>>> entry = await client.core.add_bytes(b'ABCD')
{'Name': 'QmZ655k2oftYnsocBxqTWzDer3GNui2XQTtcA4ZUbhpz5N', 'Hash': 'QmZ655k2oftYnsocBxqTWzDer3GNui2XQTtcA4ZUbhpz5N', 'Size': '12'}

Adding string data

Add a UTF-8 string with api.CoreAPI.add_str():

entry = await client.core.add_str('ABCD')

Getting IPFS objects

Download IPFS objects with api.CoreAPI.get():

await client.core.get('QmRGqvWK44oWu8re5whp43P2M7j5XEDLHmPB3wncYFmCNg')

await client.core.get('QmRGqvWK44oWu8re5whp43P2M7j5XEDLHmPB3wncYFmCNg',
    dstdir='/tmp')

Cat

Use api.CoreAPI.cat() to get an object’s raw data:

bytes = await client.core.cat(cid)

Listing a path or CID

Use api.CoreAPI.ls() for listing:

listing = await client.core.ls(path)

Node information

Get IPFS node information:

info = await client.core.id()

Block API

aioipfs.AsyncIPFS.block

Gives access to the BlockAPI

Bitswap API

aioipfs.AsyncIPFS.bitswap

Gives access to the BitswapAPI

Bootstrap API

aioipfs.AsyncIPFS.bootstrap

Gives access to the BootstrapAPI

Config API

aioipfs.AsyncIPFS.config

Gives access to the ConfigAPI

CID API

aioipfs.AsyncIPFS.cid

Gives access to the CidAPI

DAG API

aioipfs.AsyncIPFS.dag

Gives access to the DagAPI

DHT API

aioipfs.AsyncIPFS.dht

Gives access to the DhtAPI

Diag API

aioipfs.AsyncIPFS.diag

Gives access to the DiagAPI

File API

aioipfs.AsyncIPFS.file

Gives access to the FileAPI

Files API

aioipfs.AsyncIPFS.files

Gives access to the FilesAPI

Filestore API

aioipfs.AsyncIPFS.filestore

Gives access to the FilestoreAPI

Key API

aioipfs.AsyncIPFS.key

Gives access to the KeyAPI

Log API

aioipfs.AsyncIPFS.log

Gives access to the LogAPI

Access the IPFS event log with:

import pprint
async for msg in client.log.tail():
    print(pprint.pprint(msg))

Multibase API

aioipfs.AsyncIPFS.multibase

Gives access to the MultibaseAPI

Name API

aioipfs.AsyncIPFS.name

Gives access to the NameAPI

Object API

aioipfs.AsyncIPFS.object

Gives access to the ObjectAPI

P2P API

aioipfs.AsyncIPFS.p2p

Gives access to the P2PAPI

Pin API

aioipfs.AsyncIPFS.pin

Gives access to the PinAPI

Pinning a CID or an IPFS path:

async for pinned in client.pin.add(cid):
    print('Pin progress', pinned['Progress'])

Listing the pinned objects:

pinned = await client.pin.ls()

Unpin with:

await client.pin.rm(path)

Pin remote API

aioipfs.AsyncIPFS.pin.remote

Gives access to the PinRemoteAPI

Pubsub API

aioipfs.AsyncIPFS.pubsub

Gives access to the PubsubAPI

Refs API

aioipfs.AsyncIPFS.refs

Gives access to the RefsAPI

Repo API

aioipfs.AsyncIPFS.repo

Gives access to the RepoAPI

Routing API

aioipfs.AsyncIPFS.routing

Gives access to the RoutingAPI

Swarm API

aioipfs.AsyncIPFS.swarm

Gives access to the SwarmAPI

Swarm Peering API

aioipfs.AsyncIPFS.swarm.peering

Gives access to the SwarmPeeringAPI

TAR API

aioipfs.AsyncIPFS.tar

Gives access to the TarAPI

Indices and tables