Search code examples
nearprotocol

Property 'headers' is missing in type when initial near api connection


I got this error when init contract in my React project. It showed when i using .tsx file. After checked the documentation from near-api-js and it didn't explain what is the headers, but when i go inside the package it actually have the 'headers' inside near.d.ts.

  

const nearConfig = getConfig(process.env.NEAR_ENV || 'testnet');
const keyStore = new nearAPI.keyStores.BrowserLocalStorageKeyStore();
const near = await nearAPI.connect({ keyStore, ...nearConfig });

enter image description here


Solution

  • I'm guessing you are missing one property in your nearConfig object, which is headers. The property can be an empty object ({}.)

    I think you can add headers:{} when you connect to the nearAPI, like this:

    const near = await nearAPI.connect({ keyStore, headers: {}, ...nearConfig });
    

    Or, you can add headers in your getConfig() function.

    The reason you need to add it is because the TypeScript definition of ConnectConfig requires it to be present.

    From the type definition in near-api-js:

    /**
     * NEAR RPC API headers. Can be used to pass API KEY and other parameters.
     * @see {@link JsonRpcProvider.JsonRpcProvider | JsonRpcProvider}
     */
    headers: {
        [key: string]: string | number;
    };