Skip to content

Commit 3adc26b

Browse files
authored
feat: open router proxy (#3)
* proxy requests to open router * rewrite from native http to fastify * read cost from response * charge by real api cost * fix port and host * refactor read response to copy of stream in text consumer * fix encoding by disable it * charge usage for stream response * empty input & allow run only in standby mode * update readme * fix: per code review * feat: improve readme and add 10% fee * fix: writing style * fix: actor name
1 parent 5f40fcc commit 3adc26b

File tree

5 files changed

+902
-71
lines changed

5 files changed

+902
-71
lines changed

.actor/actor.json

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
{
2-
"actorSpecification": 1,
3-
"name": "actor-openrouter-proxy",
4-
"title": "Standby Actor in TypeScript",
5-
"description": "Standby Actor in TypeScript.",
6-
"version": "0.0",
7-
"buildTag": "latest",
8-
"usesStandbyMode": true,
9-
"meta": {
10-
"templateId": "ts-standby"
11-
},
12-
"dockerfile": "../Dockerfile"
13-
}
2+
"actorSpecification": 1,
3+
"name": "actor-openrouter-proxy",
4+
"title": "OpenRouter Proxy",
5+
"description": "Proxy with pay per event pricing",
6+
"version": "0.0",
7+
"buildTag": "latest",
8+
"usesStandbyMode": true,
9+
"meta": {
10+
"templateId": "ts-standby"
11+
},
12+
"dockerfile": "../Dockerfile",
13+
"environmentVariables": {
14+
"OPENROUTER_API_KEY": "@openRouterKey"
15+
},
16+
"input": {
17+
"title": "Proxy OpenRouter",
18+
"description": "",
19+
"type": "object",
20+
"schemaVersion": 1,
21+
"properties": {},
22+
"required": []
23+
}
24+
}

README.md

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,96 @@
1-
## Standby TypeScript template
1+
# OpenRouter Proxy
22

3-
Start a new [web scraping](https://apify.com/web-scraping) project quickly and easily in TypeScript (Node.js) with our Standby project template. It provides a basic structure for building an Actor with [Apify SDK](https://docs.apify.com/sdk/js/) and allows you to easily add your own functionality.
3+
This Apify Actor creates a proxy for the Open Router API, allowing you to access multiple AI models through a unified OpenAI-compatible interface. All requests are charged to your Apify account on a pay-per-event basis.
44

5-
## Included features
5+
## What this Actor does
66

7-
- **[Apify SDK](https://docs.apify.com/sdk/js/)** - a toolkit for building [Actors](https://apify.com/actors)
7+
- **Proxy access**: Routes your API requests to Open Router's extensive collection of AI models
8+
- **OpenAI compatibility**: Works seamlessly with the OpenAI SDK and any OpenAI-compatible client
9+
- **Transparent billing**: Charges are applied to your Apify account at the same rates as Open Router
10+
- **Full feature support**: Supports both streaming and non-streaming responses
11+
- **No API key management**: Uses your Apify token for authentication - no need to manage separate Open Router API keys
812

9-
## Resources
13+
## Pricing
1014

11-
- [Actor Standby documentation](https://docs.apify.com/platform/actors/development/programming-interface/standby)
15+
This Actor uses a pay-per-event pricing model through Apify. Each API request counts as one event. The underlying Open Router API costs are included in the per-event pricing, plus a 10% fee to cover the cost of running the proxy server.
1216

17+
## Quick start
1318

14-
## Getting started
15-
16-
For complete information [see this article](https://docs.apify.com/platform/actors/development#build-actor-locally). To run the Actor use the following command:
19+
### 1. Install the OpenAI package
1720

1821
```bash
19-
apify run
22+
npm install openai
2023
```
2124

22-
## Deploy to Apify
23-
24-
### Connect Git repository to Apify
25-
26-
If you've created a Git repository for the project, you can easily connect to Apify:
27-
28-
1. Go to [Actor creation page](https://console.apify.com/actors/new)
29-
2. Click on **Link Git Repository** button
25+
### 2. Basic usage
26+
27+
```javascript
28+
import OpenAI from 'openai';
29+
30+
const openai = new OpenAI({
31+
baseURL: 'https://michal-kalita--openrouter-proxy.apify.actor/api/v1',
32+
apiKey: 'no-key-required-but-must-not-be-empty', // Any non-empty string works; do NOT use a real API key.
33+
defaultHeaders: {
34+
Authorization: `Bearer ${process.env.APIFY_TOKEN}`, // Apify token is loaded automatically in runtime
35+
},
36+
});
37+
38+
async function main() {
39+
const completion = await openai.chat.completions.create({
40+
model: 'openrouter/auto',
41+
messages: [
42+
{
43+
role: 'user',
44+
content: 'What is the meaning of life?',
45+
},
46+
],
47+
});
48+
49+
console.log(completion.choices[0].message);
50+
}
51+
52+
await main();
53+
```
3054

31-
### Push project on your local machine to Apify
55+
### 3. Streaming responses
56+
57+
```javascript
58+
const stream = await openai.chat.completions.create({
59+
model: 'openrouter/auto',
60+
messages: [
61+
{
62+
role: 'user',
63+
content: 'Write a short story about a robot.',
64+
},
65+
],
66+
stream: true,
67+
});
68+
69+
for await (const chunk of stream) {
70+
process.stdout.write(chunk.choices[0]?.delta?.content || '');
71+
}
72+
```
3273
33-
You can also deploy the project on your local machine to Apify without the need for the Git repository.
74+
## Available models
3475
35-
1. Log in to Apify. You will need to provide your [Apify API Token](https://console.apify.com/account/integrations) to complete this action.
76+
This proxy supports all models available through Open Router from providers including:
77+
- OpenAI
78+
- Anthropic
79+
- Google
80+
- Meta
81+
- Perplexity
82+
- And many more...
3683
37-
```bash
38-
apify login
39-
```
84+
For a complete list of available models, visit [Open Router's models page](https://openrouter.ai/models).
4085
41-
2. Deploy your Actor. This command will deploy and build the Actor on the Apify Platform. You can find your newly created Actor under [Actors -> My Actors](https://console.apify.com/actors?tab=my).
86+
## Authentication
4287
43-
```bash
44-
apify push
45-
```
88+
The Actor uses your Apify token for authentication. In Apify Actor environments, `APIFY_TOKEN` is automatically available. For local development, you can:
4689
47-
## Documentation reference
90+
1. Set the environment variable: `export APIFY_TOKEN=your_token_here`
91+
2. Or pass it directly in the Authorization header
92+
3. Find your token in the [Apify Console](https://console.apify.com/account/integrations)
4893
49-
To learn more about Apify and Actors, take a look at the following resources:
94+
## Support
5095
51-
- [Apify SDK for JavaScript documentation](https://docs.apify.com/sdk/js)
52-
- [Apify SDK for Python documentation](https://docs.apify.com/sdk/python)
53-
- [Apify Platform documentation](https://docs.apify.com/platform)
54-
- [Join our developer community on Discord](https://discord.com/invite/jyEM2PRvMU)
96+
For issues related to this Actor, please contact the Actor developer.

0 commit comments

Comments
 (0)