Guides
Getting Started
Learn how to get started with Unreal OpenAI API
Getting Started with Unreal OpenAI Backend
This guide will help you get started with the Unreal's OpenAI API, which is compatible with the OpenAI SDK.
Prerequisites
Before you begin, make sure you have:
- An Unreal API key (generate one in the Unreal Console)
- Node.js installed (version 16 or higher)
- Basic knowledge of JavaScript/TypeScript
Installation
Install the OpenAI SDK which will be used to interact with OpenRouter:
# Using npm
npm install openai
# Using yarn
yarn add openai
# Using bun
bun add openaiAuthentication
To authenticate with the OpenRouter API, you need to provide your API key when initializing the OpenAI client:
import { OpenAI } from 'openai';
const openai = new OpenAI({
apiKey: 'your-openrouter-api-key', // Replace with your actual API key
baseURL: OPENAI_URL, // OpenRouter API endpoint
});Making Your First API Call
Chat Completions
Here's a simple example of how to use the chat completions endpoint:
async function getChatCompletion() {
const completion = await openai.chat.completions.create({
model: 'your-preferred-model', // Replace with your preferred model
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is OpenRouter?' }
],
});
console.log(completion.choices[0].message.content);
}
getChatCompletion();Image Generation
You can also generate images using the OpenRouter API:
async function generateImage() {
const response = await openai.images.generate({
model: 'your-preferred-image-model',
prompt: 'A beautiful sunset over the ocean',
n: 1,
size: '1024x1024',
});
console.log(response.data[0].url);
}
generateImage();Error Handling
It's important to implement proper error handling in your applications:
async function safeApiCall() {
try {
const completion = await openai.chat.completions.create({
model: 'your-preferred-model',
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'Hello, how are you?' }
],
});
return completion.choices[0].message.content;
} catch (error) {
console.error('Error calling OpenRouter API:', error);
return null;
}
}Next Steps
Now that you've made your first API call, you can:
- Explore the API Reference for detailed information about all available endpoints
- Check out our Examples for more code samples
- Learn about Advanced Features for more complex use cases