Search code examples
apigoipfs

Can I use go-ipfs directly in my Go program?


I connect to IPFS via go-ifps-api like in this example:

 import api "github.com/ipfs/go-ipfs-api"

 shell := api.NewShell("localhost:5001")
 cid, err := shell.Add(bytes.NewBufferString("Hello world"))
 ...
 err = shell.Get(cid, "hello.txt")
 ...

However, if someone downloads such a program, they have to install go-ipfs and also have the 5001 port free. I understand that port can be flexible, but then it is one more complication.

I would like users to connect to IPFS transparently without setting up an API service, so the IPFS node is started and stopped from inside of a Go application.

Is it possible, and if yes, can you provide an example?


Solution

  • Yes, you can use go-ipfs as a library. There are some examples here https://github.com/ipfs/go-ipfs/tree/90a573354af23e361da87a29f7bafc459967c070/docs/examples. Basically you need to import go-ipfs (and some of its subpackages) instead of go-ipfs-api.