Search code examples
ipfsjs-ipfs

How to connect to IPFS node started programmatically using ipfs-core from a java server?


I am programmatically starting an IPFS node using JS ipfs-core(npm package) with a custom repository using a different storage backend(similar to S3). Now once the node is started in the AWS instance, I want to send requests to the node using a remote client written in Java.

java-ipfs-http-client can connect to the API port. But, the API and gateway service does not get initiated when the node is started. The Java server will be running on a different machine.

Is it possible to access the ipfs node started using ipfs-core programmatically from a java server running on a different instance?


Solution

  • Found the solution.

    When we initialize node programmatically, we need to manually start API/Gateway in the following way.

    import * as IPFS from 'ipfs-core'
    import { HttpApi } from 'ipfs-http-server'
    import { HttpGateway } from 'ipfs-http-gateway'
    
    async function startIpfsNode () {
      const ipfs = await IPFS.create()
    
      const httpApi = new HttpApi(ipfs)
      await httpApi.start()
    
      const httpGateway = new HttpGateway(ipfs)
      await httpGateway.start()
    }
    
    startIpfsNode()
    

    This will start the ipfs node along with the API and Gateway

    The configuration of API and Gateway port can be changed programmatically in the following way

    const ipfs = IPFS.create()
    await ipfs.config.set('Addresses.API', '/ip4/127.0.0.1/tcp/5002');
    await ipfs.config.set('Addresses.Gateway', '/ip4/127.0.0.1/tcp/9090');
    

    Once the API is started, the IPFS node can accessed from a Java Program using java-ipfs-http-client