I'm getting an error while generating key after uploading file into IPFS infura.

Sample code given below

var ipfsAPI = require('ipfs-http-client');
const ipfs = ipfsAPI('ipfs.infura.io', '5001', { protocol: 'https' });
var oldpath = files[property].path;
var uploadedFile = fs.readFileSync(oldpath);
const filess = [{
                    path: files[property].name,
                    content: ipfs.types.Buffer.from(uploadedFile)
 ipfs.add(filess, function (err, files) {

                  if (err || !files) {
                        return reject('upload failed');
 var obj;
for (i = 0; i < data.length; i++) {
    obj = { "id": i, "hash": data[i], "EID": formFields.EID };
ipfs.dag.put(tt, {}, (err, cid) => {
    const adr = cid.toBaseEncodedString();
    var dTimeStamp = new Date().getTime();
ipfs.key.gen(dTimeStamp, {
type: 'rsa',
size: 2048
}, (err, key) => {
    if (err != null) {

    console.log("ipfs Key: ", key.id + " key name : " + key.name);

    var keyVall = key.id;

ipfs.name.publish(adr, { key: keyVall }, (err, pVal) => {

        console.log("published hash : " + pVal);

        var ipfsHash = pVal.name;

    return resp.json({ status: 'SUCCESS', message: 'user has been created successfully', digitalID: pVal });


i'm getting a following error while generating key

"SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at streamToValue (C:\Service\node_modules\ipfs-http-client\src\utils\stream-to-json-value.js:25:18)
    at concat (C:Service\node_modules\ipfs-http-client\src\utils\stream-to-value.js:12:22)
    at ConcatStream.<anonymous> (C:Service\node_modules\concat-stream\index.js:37:43)
    at ConcatStream.emit (events.js:187:15)
    at finishMaybe (C:Service\node_modules\readable-stream\lib\_stream_writable.js:630:14)
    at afterWrite (C:Service\node_modules\readable-stream\lib\_stream_writable.js:492:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)"

Any leads will be appreciated.


Infura IPFS service only allows a small set of IPFS API methods through their Gateway so you cannot generate a key via ipfs.key.gen

See Infura Documentation

enter image description here

I would recommend to run your own IPFS node, especially if you want to generate and store keypair.

  • thanks for the information. Do you have any documentation to run own IPFS node on windows server?
    – user324534
    Jan 29 '19 at 5:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.