Tutorials

Free, Unlimited OpenAI API

This tutorial will show you how to use Puter.js to access OpenAI API capabilities for free, without needing an OpenAI API key. Puter.js is completely free and open-source, allowing you to provide your users with powerful AI capabilities without any API keys or usage restrictions. Using Puter.js, you can access GPT-5, GPT-4o, GPT-4.1, GPT-4.5, o1, o3, o4, GPT Image, DALL-E, ... directly from your frontend code without any server-side setup.

Puter is the pioneer of the "User Pays" model, which allows developers to incorporate AI capabilities into their applications while each user will cover their own usage costs. This model enables developers to offer advanced AI capabilities to users at no cost to themselves, without any API keys or server-side setup.

Getting Started

You can use puter.js without any API keys or sign-ups. To start using Puter.js, include the following script tag in your HTML file, either in the <head> or <body> section:

<script src="https://js.puter.com/v2/"></script>

Nothing else is required to start using Puter.js for free access to OpenAI API models and capabilities.

Example 1Use gpt-5-nano for text generation

To generate text using GPT-5 nano, use the puter.ai.chat() function:

puter.ai.chat("What are the benefits of exercise?", { model: "gpt-5-nano" })
    .then(response => {
        puter.print(response);
    });

Full code example:

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        puter.ai.chat("What are the benefits of exercise?", { model: "gpt-5-nano" })
            .then(response => {
                puter.print(response);
            });
    </script>
</body>
</html>

Example 2Generate images with GPT Image

To create images using GPT Image, use the puter.ai.txt2img() function:

puter.ai.txt2img("A futuristic cityscape at night", { model: "gpt-image-1" })
    .then(imageElement => {
        document.body.appendChild(imageElement);
    });

Full code example:

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        puter.ai.txt2img("A futuristic cityscape at night", { model: "gpt-image-1" })
            .then(imageElement => {
                document.body.appendChild(imageElement);
            });
    </script>
</body>
</html>

Example 3Analyze images

To analyze images, simply provide an image URL to puter.ai.chat():

puter.ai.chat(
    "What do you see in this image?", 
    "https://assets.puter.site/doge.jpeg",
    { model: "gpt-5-nano" }
)
.then(response => {
    puter.print(response);
});

Full code example:

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        puter.ai.chat(
            "What do you see in this image?", 
            "https://assets.puter.site/doge.jpeg",
            { model: "gpt-5-nano" }
        )
        .then(response => {
            puter.print(response);
        });
    </script>
</body>
</html>

Example 4Use different OpenAI models

You can specify different OpenAI models using the model parameter, for example gpt-5, gpt-5-nano, gpt-5-mini, gpt-5-chat-latest:

// Using gpt-5 model
puter.ai.chat(
    "Write a short poem about coding",
    { model: "gpt-5" }
).then(response => {
    puter.print(response);
});

// Using gpt-5-nano model
puter.ai.chat(
    "Write a short poem about coding",
    { model: "gpt-5-nano" }
).then(response => {
    puter.print(response);
});

// Using gpt-5-mini model
puter.ai.chat(
    "Write a short poem about coding",
    { model: "gpt-5-mini" }
).then(response => {
    puter.print(response);
});

// Using gpt-5-chat-latest model
puter.ai.chat(
    "Write a short poem about coding",
    { model: "gpt-5-chat-latest" }
).then(response => {
    puter.print(response);
});

Full code example:

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        // Using gpt-5 model
        puter.ai.chat(
            "Write a short poem about coding",
            { model: "gpt-5" }
        ).then(response => {
            puter.print("<h2>Using gpt-5 model</h2>");
            puter.print(response);
        });

        // Using gpt-5-nano model
        puter.ai.chat(
            "Write a short poem about coding",
            { model: "gpt-5-nano" }
        ).then(response => {
            puter.print("<h2>Using gpt-5-nano model</h2>");
            puter.print(response);
        });

        // Using gpt-5-mini model
        puter.ai.chat(
            "Write a short poem about coding",
            { model: "gpt-5-mini" }
        ).then(response => {
            puter.print("<h2>Using gpt-5-mini model</h2>");
            puter.print(response);
        });

        // Using gpt-5-chat-latest model
        puter.ai.chat(
            "Write a short poem about coding",
            { model: "gpt-5-chat-latest" }
        ).then(response => {
            puter.print("<h2>Using gpt-5-chat-latest model</h2>");
            puter.print(response);
        });
    </script>
</body>
</html>

Example 5Stream responses for longer queries

For longer responses, use streaming to get results in real-time:

async function streamResponse() {
    const response = await puter.ai.chat(
        "Explain the theory of relativity in detail", 
        {stream: true}
    );
    
    for await (const part of response) {
        puter.print(part?.text);
    }
}

streamResponse();

Full code example:

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        async function streamResponse() {
            const response = await puter.ai.chat(
                "Explain the theory of relativity in detail", 
                {stream: true, model: "gpt-5-nano"}
            );
            
            for await (const part of response) {
                puter.print(part?.text);
            }
        }

        streamResponse();
    </script>
</body>
</html>

Example 6Control randomness and length with `temperature` and `max_tokens`

To control randomness and length, you can use the temperature and max_tokens parameters in the options object:

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            // Low temperature (0.2) for focused, deterministic output
            const focused = await puter.ai.chat(
                'Tell me about planet Mars', 
                { 
                    temperature: 0.2,
                    max_tokens: 10 
                }
            );
            puter.print('<b>Low temperature (0.2), max_tokens: 10:</b><br>' + focused + '<br><br>');
            
            // High temperature (0.8) for creative, varied output  
            const creative = await puter.ai.chat(
                'Tell me about planet Mars',
                { 
                    temperature: 0.8,
                    max_tokens: 50
                }
            );
            puter.print('<b>High temperature (0.8), max_tokens: 50:</b><br>' + creative);
        })();
    </script>
</body>
</html>

This example shows how temperature affects output randomness (lower = more focused, higher = more creative) and how max_tokens limits the response length.

Example 7Tool/Function Calling

Here's a concise section for tool/function calling:

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        // Define a calculator tool
        const tools = [{
            type: "function",
            function: {
                name: "calculate",
                description: "Perform basic math operations",
                parameters: {
                    type: "object",
                    properties: {
                        operation: {
                            type: "string",
                            enum: ["add", "subtract", "multiply", "divide"]
                        },
                        a: { type: "number" },
                        b: { type: "number" }
                    },
                    required: ["operation", "a", "b"]
                }
            }
        }];

        // Ask the AI to use the tool
        puter.ai.chat("What is 15 multiplied by 7?", { tools }).then(response => {
            if (response.message.tool_calls) {
                const call = response.message.tool_calls[0];
                const args = JSON.parse(call.function.arguments);
                
                // Execute the function based on the AI's request
                let result;
                if (args.operation === "multiply") {
                    result = args.a * args.b;
                }
                
                puter.print(`AI requested: ${args.a} × ${args.b} = ${result}`);
            }
        });
    </script>
</body>
</html>

This example shows how to define tools that the AI can call and process the function calls in your code.

List of supported text generation models

The following OpenAI models are supported by Puter.js, which can be used with the puter.ai.chat() function:

gpt-5
gpt-5-mini
gpt-5-nano
gpt-5-chat-latest
gpt-4.1
gpt-4.1-mini
gpt-4.1-nano
gpt-4.5-preview
gpt-4o
gpt-4o-mini
o1
o1-mini
o1-pro
o3
o3-mini
o4-mini

List of supported image generation models

The following GPT Image models are supported by Puter.js, which can be used with the puter.ai.txt2img() function:

gpt-image-1
dall-e-3
dall-e-2



That's it! You now have a free alternative to the OpenAI API using Puter.js. This allows you to access GPT-5, GPT-4o, o4, o3-mini, o1-mini, DALL-E, ... capabilities without needing an API key or a backend. True serverless AI!

Related

Free, Serverless AI and Cloud

Start creating powerful web applications with Puter.js in seconds!

Get Started Now

Read the Docs Try the Playground