title: How do you include an SSL certificate in an API request in Node?
body: <p>Here is the situation: I have a certificate, key, public key, and private key. I am building an app that integrates with another system. My system is a Next.js app running on Azure. It makes API requests to an external server running somewhere else.  <strong>I know that my certificate/keys are good because the request works when I use the Insomnia API Client (200 response and data coming back).</strong></p>
  <p>However, trying to do this in Node, I keep getting 400 errors. I have tried doing this with core <code>fetch</code> and <code>https</code> functionalities and using the <code>node-fetch</code> package.  I also looked at the <code>request</code> package (as there are examples of this on the internet), but that package was deprecated over a year ago, so I think it should not be used.</p>
  <p>Here's an example of some of the code I've tried using. I'm storing the certs/keys in <code>/tmp/certs</code> on my local system &amp; they are loading (I've been logging heavily). I've used a wide variety of different <code>requestOptions</code>, but nothing seems to be doing the trick.</p>
  <pre><code>import fs from 'fs'
  import fetch from 'node-fetch'
  
  const requestOptions = {
      method: 'GET',
      port: 443,
      cert: fs.readFileSync(&quot;/tmp/certs/ct.crt&quot;, 'utf-8'),
      key: fs.readFileSync(&quot;/tmp/certs/ct.key&quot;, 'utf-8')
    };
  
  const res = await fetch(&quot;https://url.com?unique_ID=1234&amp;key=abcd&quot;, requestOptions);
  </code></pre>
  <p>Examining the <code>res</code> always turns up a 400.  I have tried doing this a number of different ways at this point. This seems like it should be a trivial, common use case... What am I doing wrong and <strong>how should I be structuring this request?</strong></p>
  <p>Help me, Stack Overflow, you're my only hope!</p>