<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Generative-AI-Js]]></title><description><![CDATA[Generative-AI-Js]]></description><link>https://generative-ai-js.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 09 Oct 2026 08:38:04 GMT</lastBuildDate><atom:link href="https://generative-ai-js.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🌐 Using OpenRouter Free & Paid Models with Node.js]]></title><description><![CDATA[If you’re just getting started with Generative AI and don’t want to burn through paid API credits, you can experiment with OpenRouter. OpenRouter provides access to multiple AI models, including free ones like DeepSeek Chat v3.1 (free).
In this guide...]]></description><link>https://generative-ai-js.hashnode.dev/using-openrouter-free-and-paid-models-with-nodejs</link><guid isPermaLink="true">https://generative-ai-js.hashnode.dev/using-openrouter-free-and-paid-models-with-nodejs</guid><category><![CDATA[AI]]></category><category><![CDATA[generative ai]]></category><category><![CDATA[ai agents]]></category><dc:creator><![CDATA[Puneet]]></dc:creator><pubDate>Fri, 05 Sep 2025 17:59:14 GMT</pubDate><content:encoded><![CDATA[<p>If you’re just gett<a target="_blank" href="https://openrouter.ai/">ing starte</a>d with <strong>Generative AI</strong> and don’t want to burn through paid API credits, you can experiment with <strong>OpenRouter</strong>. OpenRouter provides access to multiple AI models, including free ones like <strong>DeepSeek Chat v3.1 (free)</strong>.</p>
<p>In this guide, we’l<a target="_blank" href="https://openrouter.ai/">l cover:</a></p>
<p>✅ How to set up Ope<a target="_blank" href="https://openrouter.ai/">nRouter wi</a>th Node.js<br />✅ Example code to call a free model<br />✅ How to manage your API key<br />✅ Limitations &amp; best practices<br />✅ How tokens work in <strong>free vs. paid models</strong></p>
<hr />
<h2 id="heading-1-get-your-openrouter-api-khttpsopenrouteraiey">1️⃣ Get Your OpenRo<a target="_blank" href="https://openrouter.ai/">uter API K</a>ey</h2>
<ol>
<li><p>Go to <a target="_blank" href="https://openrouter.ai/">OpenRouter</a>.</p>
</li>
<li><p><a target="_blank" href="https://openrouter.ai/">Sign up</a> with your e<a target="_blank" href="https://openrouter.ai/">mail or Gi</a>tHub/Google account.</p>
</li>
<li><p>Head to the <strong>API Key</strong><a target="_blank" href="https://openrouter.ai/"><strong>s</strong> section.</a></p>
</li>
<li><p>Copy your API key a<a target="_blank" href="https://openrouter.ai/">nd save it</a> in a <code>.env</code> file.</p>
</li>
</ol>
<p>Example <code>.env</code> file:</p>
<pre><code class="lang-bash">OPENAI_API_KEY=your-openrouter-api-key
</code></pre>
<p>⚠️ Even though it’s <a target="_blank" href="https://openrouter.ai/">free, kee</a>p your key secret — don’t commit it to GitHub.</p>
<hr />
<h2 id="heading-2-set-up-your-nodejs-projehttpsopenrouteraict">2️⃣ Set Up Your Nod<a target="_blank" href="https://openrouter.ai/">e.js Proje</a>ct</h2>
<p>Install dependencie<a target="_blank" href="https://openrouter.ai/">s:</a></p>
<pre><code class="lang-bash">npm install dotenv node-fetch
</code></pre>
<p>Project structure:</p>
<pre><code class="lang-plaintext">📂 gen-ai
 ┣ 📂 src
 ┃ ┗ index.js
 ┣ .env
 ┣ package.json
</code></pre>
<hr />
<h2 id="heading-3-example-code-for-a-free-mhttpsopenrouteraiodel">3️⃣ Example Code fo<a target="_blank" href="https://openrouter.ai/">r a Free M</a>odel</h2>
<p>Here’s a working <strong>in</strong><a target="_blank" href="https://openrouter.ai/"><strong>dex.js</strong> exa</a>mple:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> dotenv <span class="hljs-keyword">from</span> <span class="hljs-string">"dotenv"</span>; <span class="hljs-comment">// Loads environment variables from .env file</span>
<span class="hljs-keyword">import</span> fetch <span class="hljs-keyword">from</span> <span class="hljs-string">"node-fetch"</span>; <span class="hljs-comment">// Imports fetch for making HTTP requests</span>

dotenv.config(); <span class="hljs-comment">// Initializes dotenv to use environment variables</span>

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Sends a POST request to OpenRouter's chat completions endpoint</span>
  <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(
    <span class="hljs-string">"https://openrouter.ai/api/v1/chat/completions"</span>,
    {
      <span class="hljs-attr">method</span>: <span class="hljs-string">"POST"</span>, <span class="hljs-comment">// HTTP method</span>
      <span class="hljs-attr">headers</span>: {
        <span class="hljs-attr">Authorization</span>: <span class="hljs-string">`Bearer <span class="hljs-subst">${process.env.OpenAI_API_KEY}</span>`</span>, <span class="hljs-comment">// Authenticates using API key from .env</span>
        <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/json"</span>, <span class="hljs-comment">// Specifies JSON payload</span>
        <span class="hljs-comment">// "HTTP-Referer": "&lt;YOUR_SITE_URL&gt;", // Optional: site URL for OpenRouter rankings</span>
        <span class="hljs-comment">// "X-Title": "&lt;YOUR_SITE_NAME&gt;", // Optional: site name for OpenRouter rankings</span>
      },
      <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify({
        <span class="hljs-attr">model</span>: <span class="hljs-string">"deepseek/deepseek-chat-v3.1:free"</span>, <span class="hljs-comment">// Specifies the model to use</span>
        <span class="hljs-attr">messages</span>: [
          <span class="hljs-comment">//   { role: "system", content: "You are a helpful assistant." }, // Optional system prompt</span>
          <span class="hljs-comment">//   { role: "user", content: "What is the capital of France?" }, // Example user prompt</span>
        ],
      }),
    }
  );

  <span class="hljs-comment">// Parses the JSON response from the API</span>
  <span class="hljs-keyword">const</span> completion = <span class="hljs-keyword">await</span> response.json();
  <span class="hljs-comment">// Logs the assistant's reply to the console</span>
  <span class="hljs-built_in">console</span>.log(completion.choices[<span class="hljs-number">0</span>].message.content);
}

main(); <span class="hljs-comment">// Runs the main function</span>
</code></pre>
<p>Run the project:</p>
<pre><code class="lang-bash">node src/index.js
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">The capital of France is Paris.
</code></pre>
<hr />
<h2 id="heading-4-limitations-amp-best-practihttpsopenrouteraices">4️⃣ Limitations &amp; B<a target="_blank" href="https://openrouter.ai/">est Practi</a>ces</h2>
<ul>
<li><p><strong>Free models may hav</strong><a target="_blank" href="https://openrouter.ai/"><strong>e restrict</strong></a><strong>ions</strong> → slower response times, limited requests per day.</p>
</li>
<li><p><strong>Always check rate l</strong><a target="_blank" href="https://openrouter.ai/"><strong>imits</strong> on O</a>penRouter before scaling.</p>
</li>
<li><p><strong>Prefer paid models</strong> <a target="_blank" href="https://openrouter.ai/">(like GPT-</a>4, Claude, Gemini) for production apps.</p>
</li>
<li><p><strong>Structure your code</strong> <a target="_blank" href="https://openrouter.ai/">→ keep AP</a>I calls in separate utility files for maintainability.</p>
</li>
</ul>
<hr />
<h2 id="heading-5-understanding-tokens-frehttpsopenrouteraie-vs-paid-models">5️⃣ Understanding T<a target="_blank" href="https://openrouter.ai/">okens (Fre</a>e vs. Paid Models)</h2>
<p>OpenAI and OpenRout<a target="_blank" href="https://openrouter.ai/">er bill ba</a>sed on <strong>tokens</strong>, not words.</p>
<p>👉 A <strong>token</strong> is a chu<a target="_blank" href="https://openrouter.ai/">nk of text</a> (roughly 4 characters in English).<br />Example: <code>ChatGPT is awesome!</code> → 4 tokens (<code>Chat</code>, <code>GPT</code>, <code>is</code>, <code>awesome!</code>).</p>
<h2 id="heading-6-how-to-calculate-tokens-ihttpsopenrouterain-nodejs">6️⃣ How to Calculat<a target="_blank" href="https://openrouter.ai/">e Tokens i</a>n Node.js</h2>
<p>Install the token e<a target="_blank" href="https://openrouter.ai/">ncoder:</a></p>
<pre><code class="lang-bash">npm install js-tiktoken
</code></pre>
<p>Example usage:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { encoding_for_model } <span class="hljs-keyword">from</span> <span class="hljs-string">"js-tiktoken"</span>;

<span class="hljs-keyword">const</span> encoder = encoding_for_model(<span class="hljs-string">"gpt-4"</span>);

<span class="hljs-keyword">const</span> text = <span class="hljs-string">"ChatGPT is amazing!"</span>;
<span class="hljs-keyword">const</span> tokens = encoder.encode(text);

<span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Token count:"</span>, tokens.length);
</code></pre>
<p>Output:</p>
<pre><code class="lang-bash">Token count: 4
</code></pre>
<p><a target="_blank" href="https://openrouter.ai/">👉</a> Always track tok<a target="_blank" href="https://openrouter.ai/">en usage i</a>n <strong>paid models</strong> to estimate cost and avoid surprises.</p>
]]></content:encoded></item></channel></rss>