openrouter
Service icon

OpenRouter

Stable version 1.0.0 (Compatible with OutSystems 11)
Uploaded
 on 21 Jul
 by 
0.0
 (0 ratings)
openrouter

OpenRouter

Documentation
1.0.0

🚀Overview

The OpenRouter enables OutSystems applications to seamlessly integrate with OpenRouter’s unified API, providing access to a wide range of advanced AI models — including OpenAI, Anthropic, Google, Mistral, and more — via a single, simplified interface.

It abstracts the complexity of REST APIs, allowing developers to quickly implement features such as chatbotssummarizationtext generation, and real-time AI messaging.




📥 Installation

  1. Download the OpenRouter from OutSystems Forge.

  2. Add it as a dependency in your application:

    • In Service Studio, open Manage Dependencies.

    • Search for OpenRouter and include it in your module.




✅ Prerequisites

  • OpenRouter Account: Register here

  • API Key: Generate your API key from your OpenRouter dashboard

  • Model ID: Select an appropriate model from OpenRouter (e.g., mistralai/mixtral-8x7bmeta-llama/llama-3.1-405b-instruct, etc.)




🛠️ Usage Instructions

1. Use the Server Action

In your logic flow, drag and drop the OpenRouter Connector Server Action wherever you want to make an OpenRouter API call.




2. Provide Required Parameters

Parameter

Type

Required

Description

api_key

Text

✔️

Your OpenRouter API key

model_id

Text

✔️

The model you want to use (e.g., mistralai/mixtral-8x7bmeta-llama/llama-3.1-405b-instruct)

content_type

Text

Defaults to application/json if not specified

messages

List

✔️

List of message objects with role and content

💡 Example messages JSON:

[

  { "role": "system", "content": "You are a helpful assistant that provides clear and accurate answers to user questions." },

  { "role": "user", "content": "Who is the first Prime Minister of India?" }

]


🧠 Common roles: systemuserassistant.




3. Handle the Result Response

The Server Action returns a result property containing the AI-generated response. You can parse and bind this result in your UI.


🧪 Demo Reference: Paragraph Summarizer

The OpenRouterConnector_Demo module (available on Forge) demonstrates a working use-case: summarizing long paragraphs using an AI model from OpenRouter.




🔄 How the Demo Works

  • API Key Configuration: Set your API key in the OpenRouter_ApiKey Site Property.

  • Model Id Configuration: Users can specify the model to be used via the Model Id input field (e.g., meta-llama/llama-3.1-405b-instruct).

  • Messages Structure:

    • System message: "You are a helpful assistant that summarizes long text into a short and clear paragraph."

    • User message: The paragraph inputted by the user.




🔁 Summarization Flow

  1. The user enters a paragraph in the "Text to Summarize" input.

  2. User provides a Model ID (e.g., meta-llama/llama-3.1-405b-instruct).

  3. On clicking "Summarize", the connector is triggered and sends both messages to OpenRouter.

  4. The summarized output is shown in the "Summary" field.




🖼️ UI Layout

  • Model Id: Text input to specify the model to use for the request.

  • Text to Summarize: Large input field for entering the paragraph.

  • Summarize Button: Executes the API call.

  • Summary Output: Displays the AI-generated summary.

🧰 How to Implement the Demo in Your Own App

1. Install the Demo Module

  • Download and publish OpenRouterConnector_Demo from Forge.

2. Configure Site Properties

  • Set your API key in the OpenRouter_ApiKey Site Property.

3. Logic Flow Example

📥 Assemble the messages list:


[

  { "role": "system", "content": "You are a helpful assistant that summarizes long text into a short and clear paragraph." },

  { "role": "user", "content": "<User Input Paragraph>" }

]


▶️ Call the Connector Server Action:


api_key: GetSiteProperty("OpenRouter_ApiKey")

model_id: "meta-llama/llama-3.1-405b-instruct" (or user-selected)

messages: as above





💻 Example (Pseudocode):

// On Summarize Button Click


messages = [

  { role: "system", content: "You are a helpful assistant that summarizes long text into a short and clear paragraph." },

  { role: "user", content: InputParagraph }

]


response = OpenRouterConnector.ServerAction(

  api_key = GetSiteProperty("OpenRouter_ApiKey"),

  model_id = UserSelectedModelId,

  messages = messages

)


SummarizedResult.Text = response.result





✅ Best Practices

  • 🔐 API Key Security: Store API keys in Site Properties or environment variables. Avoid hardcoding.

  • 🗂️ Message Format: Use clear role and content structure for prompts.

  • ⚠️ Error Handling: Handle timeouts, null responses, and invalid keys gracefully.

  • 🤖 Model Selection: Choose appropriate models based on use-case. Refer to OpenRouter’s model catalog for full listings.




🛠️ Troubleshooting


Issue

Solution

Invalid API Key [401]

Ensure the key is correct and active on OpenRouter.

No Response/Timeout

Check internet connectivity and model availability.
Increase Server Request Timeout ( in secs)

Unexpected Output

Tweak the system prompt or try a different model_id.