# invokeRPC API during key generation returns {"error": {"code": -1, "message": "Unauthorized"}}}

**URL:** <https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148>\
**Category:** Support\
**Created:** [June 27, 2024, 1:38pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148 "2024-06-27T13:38:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [June 27, 2024, 1:38pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/1 "2024-06-27T13:38:29Z")

</div>

I am having issue with the response being returned from invokeRPC API. This is the error message we are getting:

{“success”: true, “message”: “OK”, “rpcResponse”: {“error”: {“code”: -1, “message”: “Unauthorized”}}}

After changing the apiKey to SIGNER\_KEY, attached is the error message we are getting:

 ![photo_2024-06-27 21.47.51](https://us1.discourse-cdn.com/flex015/uploads/fireblocks/original/1X/3c72161519093f9b3a2785ef37affd0aa18b73c6.jpeg)

---

<div class="post-metadata">

**Author:** ![mnamakwala](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mnamakwala](https://community.fireblocks.com/u/mnamakwala)\
**Post date:** [June 27, 2024, 5:30pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/2 "2024-06-27T17:30:41Z")

</div>

Hi Kenna,  
This is Mohammed from Fireblocks.  
The key generation has to be done from an end-user device.  
Please check out this comment about testing using POSTMAN

> **[Postman Guide](https://ncw-developers.fireblocks.com/docs/postman-guide#:~:text=to%20the%20following%3A-,Making%20your%20first%20request,-%F0%9F%9A%A7)**
>
> Video Guide

---

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [June 28, 2024, 3:54am UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/3 "2024-06-28T03:54:12Z")

</div>

Thank you @mnamakwala.

```auto
"rpcResponse": {
"error": {
"message": "Unauthorized",
"code": -1
}
}

```

This error what will get using the frontend (I attached frontend codes segment). The backend API logic that is returning the error is fireblockSdk.signer.NCW.invokeWalletRpc. We used secretKey and signer\_key from sanbox.

 ![image (2)](https://us1.discourse-cdn.com/flex015/uploads/fireblocks/original/1X/ff7a280b04154f496ac177016900be3971ac92b3.png)

---

<div class="post-metadata">

**Author:** ![mnamakwala](https://avatars.discourse-cdn.com/v4/letter/m/b5a626/32.png) [@mnamakwala](https://community.fireblocks.com/u/mnamakwala)\
**Post date:** [June 28, 2024, 6:15pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/4 "2024-06-28T18:15:41Z")

</div>

Please provide the workspace name and the first 4 digits of the API key being used?

---

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [June 29, 2024, 1:55pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/5 "2024-06-29T13:55:22Z")

</div>

@mnamakwala workspace name - Octoprime, API key (first 4 digit) - 6126…

---

<div class="post-metadata">

**Author:** ![golan](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@golan](https://community.fireblocks.com/u/golan)\
**Post date:** [July 1, 2024, 12:12pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/6 "2024-07-01T12:12:57Z")

</div>

Please double check how you initialize the Fireblocks API Client.  
First, your API key does not start with “6126”, the first digits are “c6126” so there’s a “c” missing.  
Second, make sure you pass the full API key (mentioned above) and the full private key. The private key should be one single string i.e. “-----BEGIN PRIVATE KEY-----\nMI…”

---

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [July 1, 2024, 1:00pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/7 "2024-07-01T13:00:37Z")

</div>

Hi @golan,

Thank you for your response. Yes the API key starts with “c”. You asked for the first 6 digits that is why we omitted the “c”. I have attached the snapshot of the begining our private key for your attention. We used as compied (we did not edit it)

 ![Screenshot 2024-07-01 at 8.58.13 PM](https://us1.discourse-cdn.com/flex015/uploads/fireblocks/original/1X/af22f006ecda07322860f1eebb0ede6e130f1d02.png)

This is how we initialized the fireblock API client

> const { FireblocksSDK } = require(‘fireblocks-sdk’);  
> const fireSignerInstance = () =\> {  
> const dirPath = path.resolve(\_\_dirname, ‘…/keys’);  
> const apiSecret = fs.readFileSync(path.join(dirPath, ‘fireblocks\_admin\_secret.key’), ‘utf8’);
> 
> ```
> const apiKey = process.env.FIREBLOCKS_API_SIGNER_KEY
> const fireblocks = new FireblocksSDK(apiSecret, apiKey, process.env.FIREBLOCKS_BASE_URL);
> 
> return fireblocks;
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![golan](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@golan](https://community.fireblocks.com/u/golan)\
**Post date:** [July 1, 2024, 3:09pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/8 "2024-07-01T15:09:18Z")

</div>

Please try replacing ending lines with ‘\n’ i.e.

```auto
let apiSecret = fs.readFileSync(path.join(dirPath, 'fireblocks_admin_secret.key'), 'utf8');
apiSecret = apiSecret.replace(/\n/g, '\\n');

```

---

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [July 2, 2024, 10:00am UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/9 "2024-07-02T10:00:22Z")

</div>

Hi @golan,

This is the error we get at the frontend after API call to the backend. The error we get on the frontend console is pasted below. Same thing we got when we tried it on postman in the attached image.

```auto
{
 "url": "https://kena.net/api/user/mpc/initialized",
 "token": "eyJhbGciOiJIUzkpXVCJ9.eyJ1c2G5vbGFuemlwLmNvbSIsImlhdCI6MTcxOTkxMjgwNzE5OTE2NDA1fQ.FmDDzB5LOL_zamC5AIoR9Db3YD4pxdFXM",
 "postData": {
  "deviceId": "515ffcce-d8ce-4f22-9730-7d39ee236911",
  "mpcMessage": "{\"method\":\"get_service_certificates\",\"headers\":{\"platformType\":\"iOS\",\"physicalDeviceId\":\"fada0fc8-a6bd-4581-837c-ea177451ab88\",\"mpcVersion\":\"6\",\"sdkVersion\":\"2.6.0\"}}",
  "userId": "ij49254@nolanzip.com"
 },
 "method": "POST"
}

```

 ![image (3)](https://us1.discourse-cdn.com/flex015/uploads/fireblocks/original/1X/f6c7ef653304bdbfdaa1b2a0c6fd38d9b0c2cf4d.png)

---

<div class="post-metadata">

**Author:** ![golan](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@golan](https://community.fireblocks.com/u/golan)\
**Post date:** [July 3, 2024, 4:06pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/10 "2024-07-03T16:06:00Z")

</div>

How do you call the FB BE from your BE?  
Can you share your code?  
Should be something like:

```auto
return await signer.NCW.invokeWalletRpc(
      walletId,
      deviceId,
      message,
    );

```

---

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [July 3, 2024, 6:14pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/11 "2024-07-03T18:14:57Z")

</div>

@golan, this is it.

> const { userId, deviceId, mpcMessage } = req.body;  
> const fireSigner = utilities.fireSignerInstance();
> 
> ```
> const invokedRPC = await fireSigner.NCW.invokeWalletRpc(account.walletId, deviceId, JSON.stringify(mpcMessage));
> console.log(`invokedRPC: ${JSON.stringify(invokedRPC)}`);
> 
> ```

Could you explain what the payload from Fireblocks to Backend represent in the diagram below?

 ![image (4)](https://us1.discourse-cdn.com/flex015/uploads/fireblocks/original/1X/3ece9eed31ddae9ebb4eeb58e606aa30cadabc05.png)

---

<div class="post-metadata">

**Author:** ![golan](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@golan](https://community.fireblocks.com/u/golan)\
**Post date:** [July 4, 2024, 8:27am UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/12 "2024-07-04T08:27:58Z")

</div>

Do you mean the webhook paylod?  
You can read our webhook documentation [here](https://ncw-developers.fireblocks.com/docs/webhook-configuration)

As for the 401 errors, we do not see any of the failures you mentioned.  
Please provide more information:

- The approximate time the API call was performed
- The requestId from the response headers if it exists.
- The fireblocks BE hostname you are using.

Also, please try to call another API, a non-NCW one, to see if you still have an 401 error, e.g. `getTransactions`

---

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [July 9, 2024, 12:09pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/13 "2024-07-09T12:09:15Z")

</div>

@golan,

- The approximate time the API call was performed - The response time is immediate.
- The requestId from the response headers if it exists. - NA
- Hostname - fireblockInstance.NCW.invokeWalletRpc (fireblockInstance was created using Signer Api\_key from our Sandbox workspace)

See below the steps from Frontend to Backend, and let us know if we missed anything.

- await FireblockService.AssignDevice() - Frontend
- await initFireblocksNCW() - Frontend
- await fireblocksNCW.generateMPCKeys() - Frontend
- await fireSigner.NCW.invokeWalletRpc (walletId, deviceId, strPayload) - Backend

initFireblocksNCW instance (frontend) is created by:

```auto
fireblocksNCW = await FireblocksNCWFactory({
          logLevel: "INFO",
          deviceId,
          messagesHandler,
          eventsHandler,
          secureStorageProvider,
        });

```

fireSigner instance (backend) is created as:

```auto
{
    const dirPath = path.resolve(__dirname, '../keys');
    let apiSecret = fs.readFileSync(path.join(dirPath, 'fireblocks_admin_secret.key'), 'utf8');
    
    apiSecret = apiSecret.replace(/\\n/g, "\n");

    const apiKey = process.env.SIGNER_API_KEY
    const fireblocks = new FireblocksSDK(apiSecret, apiKey, process.env.FIREBLOCKS_BASE_URL);
    return fireblocks;
}

```

Finally, getTransactions worked fine but the apiKey used is that of the admin (i.e., `apiKey = process.env.ADMIN_API_KEY`).

This is the final error messages that is seen at the frontend.

```auto
ERROR Request failed with status code 401
 LOG DEBUG : Error Headers: 
{
 "access-control-allow-headers": "Origin, X-Requested-With, Content-Type, Accept, Authorization",
 "access-control-allow-methods": "GET, POST, PATCH, DELETE",
 "access-control-allow-origin": "*",
 "alt-svc": "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000",
 "content-length": "65",
 "content-type": "application/json; charset=utf-8",
 "date": "Tue, 09 Jul 2024 11:20:43 GMT",
 "etag": "W/\"41-DXEuJgo1HyxuYzoDNTCOnBUGE0g\"",
 "function-execution-id": "a9ssh192ud7o",
 "server": "Google Frontend",
 "x-cloud-trace-context": "9019bc977c620559757990ab87eb5996",
 "x-powered-by": "Express"
}
 LOG [AxiosError: Request failed with status code 500] SendMessage error

```

---

<div class="post-metadata">

**Author:** ![golan](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@golan](https://community.fireblocks.com/u/golan)\
**Post date:** [July 9, 2024, 2:04pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/14 "2024-07-09T14:04:15Z")

</div>

@Kena  
You are using `fireblocks_admin_secret.key` for the `FireblocksSDK` instance of SIGNER\_API\_KEY.  
Is this correct? How did you create the NCW\_SIGNER user?  
Did you use the `fireblocks_admin_secret.key` and its corresponding CSR?

You can also try to create a new NCW SIGNER user.  
It should work for `getTransactions` as well as the admin user.

---

<div class="post-metadata">

**Author:** ![Kena](https://avatars.discourse-cdn.com/v4/letter/k/e9bcb4/32.png) [@Kena](https://community.fireblocks.com/u/Kena)\
**Post date:** [July 9, 2024, 4:25pm UTC](https://community.fireblocks.com/t/invokerpc-api-during-key-generation-returns-error-code-1-message-unauthorized/1148/15 "2024-07-09T16:25:16Z")

</div>

Thanks @golan. The issue is resolved now. It was caused because I was using the same secret key for both admin and signer.
