The dag API comes to replace the
object API
, it supports the creation and manipulation of dag-pb object, as well as other IPLD formats (i.e dag-cbor, ethereum-block, git, etc)
ipfs.dag.put(dagNode, [options])
ipfs.dag.get(cid, [options])
ipfs.dag.tree(cid, [options])
ipfs.dag.resolve(ipfsPath, [options])
Explore the DAG API through interactive coding challenges in our ProtoSchool tutorials:
- P2P data links with content addressing (beginner)
- Blogging on the Decentralized Web (intermediate)
Store an IPLD format node
Name | Type | Description |
---|---|---|
dagNode | Object |
A DAG node that follows one of the supported IPLD formats |
An optional object which may have the following keys:
Name | Type | Default | Description |
---|---|---|---|
format | String |
'dag-cbor' |
The IPLD format multicodec |
hashAlg | String |
'sha2-256' |
The hash algorithm to be used over the serialized DAG node |
cid | CID | 'dag-cbor' |
The IPLD format multicodec |
pin | boolean |
false |
Pin this node when adding to the blockstore |
timeout | Number |
undefined |
A timeout in ms |
signal | AbortSignal | undefined |
Can be used to cancel any long running requests started as a result of this call |
Note: You should pass cid
or the format
& hashAlg
pair but not both.
Type | Description |
---|---|
Promise<CID> |
A CID instance. The CID generated through the process or the one that was passed |
const obj = { simple: 'object' }
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha3-512' })
console.log(cid.toString())
// zBwWX9ecx5F4X54WAjmFLErnBT6ByfNxStr5ovowTL7AhaUR98RWvXPS1V3HqV1qs3r5Ec5ocv7eCdbqYQREXNUfYNuKG
A great source of examples can be found in the tests for this API.
Retrieve an IPLD format node
Name | Type | Description |
---|---|---|
cid | CID | A CID that resolves to a node to get |
An optional object which may have the following keys:
Name | Type | Default | Description |
---|---|---|---|
path | String |
An optional path within the DAG to resolve | |
localResolve | boolean |
false |
If set to true, it will avoid resolving through different objects |
timeout | Number |
undefined |
A timeout in ms |
signal | AbortSignal | undefined |
Can be used to cancel any long running requests started as a result of this call |
Type | Description |
---|---|
Promise<Object> |
An object representing an IPLD format node |
The returned object contains:
value
- the value or node that was fetched during the get operation.remainderPath
- The remainder of the Path that the node was unable to resolve or what was left in a localResolve scenario.
// example obj
const obj = {
a: 1,
b: [1, 2, 3],
c: {
ca: [5, 6, 7],
cb: 'foo'
}
}
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
console.log(cid.toString())
// zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5
async function getAndLog(cid, path) {
const result = await ipfs.dag.get(cid, { path })
console.log(result.value)
}
await getAndLog(cid, '/a')
// Logs:
// 1
await getAndLog(cid, '/b')
// Logs:
// [1, 2, 3]
await getAndLog(cid, '/c')
// Logs:
// {
// ca: [5, 6, 7],
// cb: 'foo'
// }
await getAndLog(cid, '/c/ca/1')
// Logs:
// 6
A great source of examples can be found in the tests for this API.
Enumerate all the entries in a graph
Name | Type | Description |
---|---|---|
cid | CID | A DAG node that follows one of the supported IPLD formats |
An optional object which may have the following keys:
Name | Type | Default | Description |
---|---|---|---|
path | String |
An optional path within the DAG to resolve | |
recursive | boolean |
false |
If set to true, it will follow the links and continuously run tree on them, returning all the paths in the graph |
timeout | Number |
undefined |
A timeout in ms |
signal | AbortSignal | undefined |
Can be used to cancel any long running requests started as a result of this call |
Type | Description |
---|---|
Promise<Array> |
An array with the paths passed |
// example obj
const obj = {
a: 1,
b: [1, 2, 3],
c: {
ca: [5, 6, 7],
cb: 'foo'
}
}
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
console.log(cid.toString())
// zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5
const result = await ipfs.dag.tree('zdpuAmtur968yprkhG9N5Zxn6MFVoqAWBbhUAkNLJs2UtkTq5')
console.log(result)
// Logs:
// a
// b
// b/0
// b/1
// b/2
// c
// c/ca
// c/ca/0
// c/ca/1
// c/ca/2
// c/cb
A great source of examples can be found in the tests for this API.
Returns the CID and remaining path of the node at the end of the passed IPFS path
Name | Type | Description |
---|---|---|
ipfsPath | String or CID |
An IPFS path, e.g. /ipfs/bafy/dir/file.txt or a CID instance |
An optional object which may have the following keys:
Name | Type | Default | Description |
---|---|---|---|
path | String |
undefined |
If ipfsPath is a CID, you may pass a path here |
timeout | Number |
undefined |
A timeout in ms |
signal | AbortSignal | undefined |
Can be used to cancel any long running requests started as a result of this call |
Type | Description |
---|---|
Promise<{ cid: CID, remainderPath: String }> |
The last CID encountered during the traversal and the path to the end of the IPFS path inside the node referenced by the CID |
// example obj
const obj = {
a: 1,
b: [1, 2, 3],
c: {
ca: [5, 6, 7],
cb: 'foo'
}
}
const cid = await ipfs.dag.put(obj, { format: 'dag-cbor', hashAlg: 'sha2-256' })
console.log(cid.toString())
// bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq
const result = await ipfs.dag.resolve(`${cid}/c/cb`)
console.log(result)
// Logs:
// {
// cid: CID(bafyreicyer3d34cutdzlsbe2nqu5ye62mesuhwkcnl2ypdwpccrsecfmjq),
// remainderPath: 'c/cb'
// }
A great source of examples can be found in the tests for this API.