Anubhav Tiwari

How to Get a Free AI API Key (Step-by-Step Guide 2026)

How to Get a Free AI API Key (Step-by-Step Guide 2026)

Looking to integrate AI in your project. But confused about how to find an api key and use it in your project. So let’s come directly to the point. If you are using it for learning or have less usage you can use openrouter.

Just visit this link https://openrouter.ai/ and sign up for a free account.

Once logged in now click on explore model.

Here you will see a list of Models available. Then on the search bar search for free. You will get a list of free AI’s you can use.

Select The Model-

Select as per your requirement whether you want text based output or image based. For My project I used Meta: Llama 3.2 3B Instruct (free)

With this I have built a simple app for leads management. In this I have passed the user message to AI and give commands to identify the message and give the output of the message. 

Like this if  lead is spam or real. So with this I manage to automate some of my team members’ tasks. To read each message and mark its status.

Get API keys

Now let’s see how we can integrate it in our project. Once you selected the AI copy the name-

meta-llama/llama-3.2-3b-instruct:free

Now to get API key, go to keys section from profile icon and Click on keys

Click on Create API key button, here you will get the API key, save it for later use.

Now let’s see the code, I have used PHP for it, you can use as per your requirement.

Step 1 Call & Pass your API key here-

$classifier = new EnquiryClassifier(OPENROUTER_API_KEY);

$result = $classifier->classify(“I want a website for my business”);

var_dump($result);

/*

[

  “type” => “real”,

  “confidence” => 0.92

]

*/

Step 2 Define Class and Build prompt

<?php

class EnquiryClassifier

{

    private string $apiKey;

    private string $model;

    private string $endpoint = ‘https://openrouter.ai/api/v1/chat/completions’;

    public function __construct(string $apiKey, string $model = ‘meta-llama/llama-3.3-70b-instruct:free’)

    {

        if (!$apiKey) {

            throw new InvalidArgumentException(‘API key is required’);

        }

        $this->apiKey = $apiKey;

        $this->model  = $model;

    }

    public function classify(string $message): ?array

    {

        $payload = [

            ‘model’ => $this->model,

            ‘messages’ => [

                [

                    ‘role’ => ‘user’,

                    ‘content’ => $this->buildPrompt($message)

                ]

            ],

            ‘temperature’ => 0.0,

            ‘max_tokens’ => 200

        ];

        $response = $this->callApi($payload);

        if (!$response) {

            return null;

        }

        return json_decode($response, true);

    }

Build Prompt here-

    private function buildPrompt(string $message): string

    {

        return <<<PROMPT

You are an assistant that classifies an enquiry message.

Return a JSON object ONLY (no explanation, no markdown) with this exact structure:

{

  “type”: “real | spam | job | other”,

  “confidence”: 0.0

}

Classification rules:

1. type = “job”

If the message is about job application, hiring, vacancy, career.

2. type = “spam”

If the message promotes unrelated services, includes suspicious links, crypto, gambling, or spammy phrases.

3. type = “real”

If the message is a genuine business enquiry for products or services.

4. type = “other”

If the message is unclear or not actionable.

Message:

\”\”\”$message\”\”\”

PROMPT;

    }

Call the API here

    private function callApi(array $payload): ?string

    {

        $ch = curl_init($this->endpoint);

        curl_setopt_array($ch, [

            CURLOPT_RETURNTRANSFER => true,

            CURLOPT_POST => true,

            CURLOPT_HTTPHEADER => [

                ‘Content-Type: application/json’,

                ‘Authorization: Bearer ‘ . $this->apiKey

            ],

            CURLOPT_POSTFIELDS => json_encode($payload),

            CURLOPT_TIMEOUT => 20

        ]);

        $response = curl_exec($ch);

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        curl_close($ch);

        if ($response === false || $httpCode < 200 || $httpCode >= 300) {

            return null;

        }

        $json = json_decode($response, true);

        return $json[‘choices’][0][‘message’][‘content’] ?? null;

    }

}

Just pass your message here –

$result = $classifier->classify(“I want a website for my business”);

And you will get the response. So with these simple steps you can integrate AI in your project.