Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules/
typescript/node_modules/
typescript/*/node_modules/
typescript/bun.lock
*.log
.DS_Store
33 changes: 33 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Makefile - Root orchestration for openrouter-examples

.PHONY: help examples typescript install clean

help:
@echo "OpenRouter Examples - Available commands:"
@echo ""
@echo " make examples - Run all examples"
@echo " make typescript - Run TypeScript monorepo examples"
@echo " make install - Install TypeScript dependencies"
@echo " make clean - Clean node_modules and lockfiles"
@echo ""

# Run all examples
examples: typescript

# Run TypeScript monorepo examples
typescript:
@echo "=== Running TypeScript examples ==="
@cd typescript && bun examples

# Install TypeScript dependencies
install:
@echo "=== Installing TypeScript dependencies ==="
@cd typescript && bun install

# Clean build artifacts
clean:
@echo "=== Cleaning TypeScript artifacts ==="
@rm -rf typescript/node_modules
@rm -rf typescript/*/node_modules
@rm -rf typescript/bun.lock
@echo "Clean complete"
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# OpenRouter Examples

Comprehensive, tested, executable examples demonstrating OpenRouter features across multiple ecosystems.

## Quick Start

```bash
# Set your API key
export OPENROUTER_API_KEY="your-key-here"

# Run all examples
make examples

# Or run specific ecosystems
make curl # Run curl examples
make typescript # Run TypeScript monorepo examples
```

## Repository Structure

```
.
├── curl/ - Shell script examples
├── typescript/ - TypeScript monorepo (Bun workspaces)
│ ├── shared/ - Shared constants and types
│ ├── fetch/ - Raw fetch API examples
│ ├── ai-sdk-v5/ - Vercel AI SDK v5 examples
│ ├── effect-ai/ - Effect-TS examples
│ └── openrouter-sdk/ - OpenRouter SDK examples (TODO)
├── docs/ - Feature documentation
└── Makefile - Unified command interface
```

## Features

### Prompt Caching
- **Documentation**: [docs/prompt-caching.md](docs/prompt-caching.md)
- **Examples**:
- [curl/prompt-caching.sh](curl/prompt-caching.sh)
- [typescript/fetch/src/prompt-caching/](typescript/fetch/src/prompt-caching/)
- [typescript/ai-sdk-v5/src/prompt-caching/](typescript/ai-sdk-v5/src/prompt-caching/)
- [typescript/effect-ai/src/prompt-caching/](typescript/effect-ai/src/prompt-caching/)

## Prerequisites

- Bun runtime: `curl -fsSL https://bun.sh/install | bash`
- OpenRouter API key: [https://openrouter.ai/keys](https://openrouter.ai/keys)
- For curl examples: `jq` (JSON processor)

## Installation

```bash
# Install TypeScript dependencies
make install

# Or manually
cd typescript && bun install
```

## Running Examples

### All Examples
```bash
make examples
```

### By Ecosystem
```bash
make curl # Shell scripts with curl + jq
make typescript # All TypeScript examples (fetch, AI SDK, Effect)
```

### Individual Examples
```bash
# curl
bash curl/prompt-caching.sh

# TypeScript
cd typescript/fetch && bun examples
cd typescript/ai-sdk-v5 && bun examples
cd typescript/effect-ai && bun examples
```

## Benefits

### For Users
1. **Copy-paste ready** - All examples are runnable as-is
2. **Tested and proven** - Every example has been verified to work
3. **Evidence-based** - Examples show expected outputs and verification
4. **Multiple ecosystems** - Choose the one that matches your stack

### For Developers
1. **Single source of truth** - Constants defined once in `typescript/shared/`
2. **Type safety** - Shared types across all TypeScript examples
3. **Consistent patterns** - Each ecosystem follows its own idioms
4. **Easy maintenance** - Bun monorepo for TypeScript workspaces

## Contributing

See individual ecosystem READMEs:
- [curl/README.md](curl/README.md)
- [typescript/README.md](typescript/README.md)

## License

See [LICENSE.md](LICENSE.md)
99 changes: 99 additions & 0 deletions biome.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"$schema": "https://biomejs.dev/schemas/2.3.3/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"files": {
"ignoreUnknown": false,
"ignore": [
"**/*.json",
"!biome.json"
]
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 100,
"attributePosition": "multiline"
},
"organizeImports": {
"enabled": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"complexity": {
"useLiteralKeys": "off",
"noExtraBooleanCast": "off",
"noForEach": "off",
"noBannedTypes": "error",
"noUselessSwitchCase": "off"
},
"style": {
"noNonNullAssertion": "off",
"useNodejsImportProtocol": "off",
"useTemplate": "off",
"useBlockStatements": "error",
"noParameterAssign": "error",
"useConst": "error",
"useAsConstAssertion": "error",
"useDefaultParameterLast": "error",
"useEnumInitializers": "error",
"useSelfClosingElements": "error",
"useSingleVarDeclarator": "error",
"noUnusedTemplateLiteral": "error",
"useNumberNamespace": "error",
"noInferrableTypes": "error",
"noUselessElse": "error",
"useImportType": "error"
},
"correctness": {
"noUnusedImports": "error",
"useExhaustiveDependencies": "off",
"noUnknownFunction": "off",
"noChildrenProp": "off",
"noInnerDeclarations": "error"
},
"suspicious": {
"noExplicitAny": "error",
"noArrayIndexKey": "off",
"noAssignInExpressions": "error",
"noAsyncPromiseExecutor": "off",
"noFallthroughSwitchClause": "error",
"noConsole": "off",
"noDoubleEquals": {
"level": "error",
"options": {
"ignoreNull": false
}
},
"noExtraNonNullAssertion": "error"
},
"performance": {
"recommended": true,
"noAccumulatingSpread": "error"
},
"security": {
"recommended": true
}
}
},
"javascript": {
"formatter": {
"lineWidth": 100,
"arrowParentheses": "always",
"jsxQuoteStyle": "single",
"attributePosition": "multiline",
"quoteProperties": "asNeeded",
"trailingCommas": "all",
"semicolons": "always",
"bracketSpacing": true,
"bracketSameLine": false,
"quoteStyle": "single"
}
}
}
13 changes: 13 additions & 0 deletions docs/prompt-caching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Prompt Caching
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: link to https://openrouter.ai/docs/features/prompt-caching instead of duplicating the content here


OpenRouter maintains the canonical documentation for prompt caching at:

- **[OpenRouter Prompt Caching Guide](https://openrouter.ai/docs/features/prompt-caching)**

That guide covers provider-specific behavior, pricing, configuration requirements, and up-to-date usage examples. This repository intentionally links out to avoid duplicating content that can drift from production behavior.

## Examples

See ecosystem-specific examples in this repository for runnable reference implementations:

- **TypeScript + fetch**: [typescript/fetch/src/prompt-caching/](../typescript/fetch/src/prompt-caching/)
51 changes: 51 additions & 0 deletions typescript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# TypeScript Examples

A Bun monorepo containing OpenRouter examples across different TypeScript ecosystems.

## Structure

- **shared/** - Shared constants and utilities (LARGE_SYSTEM_PROMPT, types, etc.)
- **fetch/** - Raw fetch API examples
- **ai-sdk-v5/** - Vercel AI SDK v5 examples (using ai v4.x package)
- **effect-ai/** - Effect-TS AI examples
- **openrouter-sdk/** - OpenRouter TypeScript SDK examples (TODO)

## Prerequisites

- Bun runtime: `curl -fsSL https://bun.sh/install | bash`
- `OPENROUTER_API_KEY` environment variable

## Installation

```bash
# From repository root
make install

# Or from the typescript/ directory
cd typescript
bun install
```

## Running Examples

```bash
# From repository root
export OPENROUTER_API_KEY="your-key-here"
make typescript

# Or from the typescript/ directory
cd typescript
bun examples

# Or run individual workspaces
cd fetch && bun examples
cd ai-sdk-v5 && bun examples
cd effect-ai && bun examples
```

## Workspace Benefits

1. **Shared constants** - LARGE_SYSTEM_PROMPT defined once in `shared/`
2. **Consistent dependencies** - Managed at monorepo root with Bun workspaces
3. **Type sharing** - Common types available across workspaces
4. **Easy testing** - Run all examples from one location with `make typescript` or `bun examples`
27 changes: 27 additions & 0 deletions typescript/fetch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# TypeScript + fetch Examples

Raw HTTP examples using TypeScript and the native `fetch` API.

## Prerequisites

- Bun runtime: `curl -fsSL https://bun.sh/install | bash`
- `OPENROUTER_API_KEY` environment variable

## Running Examples

```bash
# From monorepo root (typescript/)
bun examples

# Or from this workspace
cd fetch
bun examples
```

## Features

- [prompt-caching](./src/prompt-caching/) - Anthropic caching examples

## Dependencies

- `@openrouter-examples/shared` - Shared constants (LARGE_SYSTEM_PROMPT) and types
16 changes: 16 additions & 0 deletions typescript/fetch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@openrouter-examples/fetch",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"examples": "bun run run-examples.ts",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@openrouter-examples/shared": "workspace:*"
},
"devDependencies": {
"@types/bun": "1.3.2"
}
}
Loading