title: Using request-promise to make an API call using jwt. [ERR_INVALID_ARG_TYPE] received
body: <p>I'm learning nodejs and trying to make an API call. The API uses JWT to authenticate.</p>
  
  <p>I created these functions to sign a token:</p>
  
  <pre><code>function token() {
    const payload = {
      iat: Math.floor(new Date() / 1000),
      exp: Math.floor(new Date() / 1000) + 30,
      sub: "api_key_jwt",
      iss: "external",
      jti: crypto.randomBytes(6).toString("hex")
    };
    return new Promise((resolve, reject) =&gt; {
      jwt.sign(payload, privatekey, { algorithm: "RS256" }, function(
        err,
        token2
      ) {
        if (err) reject(err);
        else resolve(token2);
      });
    });
  }
  
  exports.genToken = async function() {
    const header = {
      "x-api-key": api
    };
    const data = {
      kid: api,
      jwt_token: await token()
    };
  
    async function authorization(req, res) {
      try {
        const auth = await rp({
          url: authurl,
          method: "POST",
          headers: header,
          body: data
        });
        res.send(auth.body);
      } catch (error) {
        res.send(404).send();
      }
    }
  
    return {
      "x-api-key": api,
      Authorization: "Bearer " + authorization()
    };
  };
  </code></pre>
  
  <p>This works fine. Then I created a function to make the API call:</p>
  
  <pre><code>const token = require("./index").genToken;
  const rp = require("request-promise");
  
  exports.getOrderBook = function(res, error) {
    const full_url = url + "order_book";
    const auth = token();
  
    rp({
      url: full_url,
      method: "GET",
      headers: auth,
      body: {
        market: "btceur"
      },
      json: true
    })
      .then(function(response) {
        res(response);
      })
      .catch(function(err) {
        error(err);
      });
  };
  </code></pre>
  
  <p>And I call it using <code>Express</code>:</p>
  
  <pre><code>routes.get("/orderbook", async (req, res, next) =&gt; {
    try {
      const book = await orders.getOrderBook();
      res.send(book);
    } catch (error) {
      next(error);
    }
  });
  </code></pre>
  
  <p>However, when I call my API, it shows an error in console: </p>
  
  <blockquote>
    <p>TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of
    type string or Buffer. Received type object.</p>
  </blockquote>
  
  <p>I guess the error is something with the token generation, because if I <code>console.log(auth)</code> in the <code>getOrderBook</code> function, it shows <code>Promise { &lt;pending&gt; }</code>, so probably an object is being passed as the jwt token.</p>
  
  <p>Is it really the problem? I tried a lot of different solutions that I found on internet, however the concept of <code>Async/Await</code> is new to me, and I'm having some troubles to figure it out.</p>
  
  <p>Thanks a lot in advance guys!</p>