fp/packages/link2cid/README.md

2.0 KiB

link2cid

Motivation

I wish I could give kubo or IPFS cluster a URI to a file and then they would download the file and add to ipfs, returning me a CID.

However, neither kubo nor IPFS cluster can do this.

link2cid solves this issue with a REST API for adding a file at url to IPFS.

Usage

Configure environment

Create a .env file. See .env.example for an example. Important environment variables are API_KEY, PORT, and IPFS_URL.

Install and run

pnpm install
pnpm start

Make a GET REST request to /add with url as a query parameter. Expect a SSE response.

dokku

dokku builder-dockerfile:set link2cid dockerfile-path link2cid.Dockerfile

Examples

HTTPIE

http -A bearer -a $API_KEY --stream 'http://localhost:3939/add?url=https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png' Accept:text/event-stream
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Cache-Control: no-cache
Connection: keep-alive
Content-Type: text/event-stream; charset=utf-8
Date: Thu, 21 Dec 2023 11:20:24 GMT
Transfer-Encoding: identity
X-Powered-By: Express

:ok

event: dlProgress
data: {
    "percent": 100
}

event: addProgress
data: {
    "percent": 100
}

event: end
data: {
    "cid": "bafkreidj3jo7efguloaixz6vgivljlmowagagjtqv4yanyqgty2hrvg6km"
}

Javascript

@todo this is incomplete/untested

await fetch('http://localhost:3939/add?url=https://upload.wikimedia.org/wikipedia/commons/7/70/Example.png', {
    headers: {
        'accept': 'text/event-stream',
        'authorization': `Bearer ${API_KEY}`
    }
});

Dev notes

Generate API_KEY

require('crypto').randomBytes(64).toString('hex')

TypeError: data.split is not a function

If you see this error, make sure data in SSE event payload is a string, not a number.