-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
235 lines (217 loc) · 7.81 KB
/
Copy pathindex.ts
File metadata and controls
235 lines (217 loc) · 7.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { type Context, Hono } from 'hono';
import './src/banner.js';
import { describeRoute, openAPIRouteHandler } from 'hono-openapi';
import { logServerInit } from './src/banner.js';
import { APP_DESCRIPTION, APP_VERSION, config } from './src/config.js';
import { logger } from './src/logger.js';
import routes from './src/routes/index.js';
import { APIErrorResponse } from './src/utils.js';
import { createX402Discovery } from './src/x402/discovery.js';
const app = new Hono();
// Tracking all incoming requests
app.use(async (c: Context, next) => {
logger.trace(`Incoming request: '${c.req.path}'`);
await next();
});
// -----------
// --- API ---
// -----------
app.route('/', routes);
// ------------
// --- Docs ---
// ------------
app.get('/', () => new Response(Bun.file('./public/index.html')));
app.get('/favicon.svg', () => new Response(Bun.file('./public/favicon.svg')));
app.get('/banner.jpg', () => new Response(Bun.file('./public/banner.jpg')));
const contentTypeMarkdown = 'text/markdown; charset=UTF-8';
const contentTypeJson = 'application/json; charset=UTF-8';
const skillsMarkdownFile = './skills/SKILL.md';
const markdownResponse = (path: string) =>
new Response(Bun.file(path), { headers: { 'Content-Type': contentTypeMarkdown } });
const skillsMarkdownOpenapi = describeRoute({
operationId: 'getSkillsMarkdown',
summary: 'Pinax API Skill',
description: 'Returns the Markdown reference for AI agents integrating with Pinax API.',
tags: ['Documentation'],
security: [],
responses: {
200: {
description: 'Successful Response',
content: {
[contentTypeMarkdown]: {
schema: {
type: 'string',
example: `---
name: pinax-api
description: Query Pinax API datasets including Token API, prediction markets, and perp exchange data.
---
# Pinax API
> Quick reference for AI agents using Pinax API. The authoritative machine-readable contract is \`GET /openapi\`.
...`,
},
},
},
},
},
});
const llmsTextOpenapi = describeRoute({
operationId: 'getLlmsText',
summary: 'LLM Documentation Index',
description: 'Returns the llms.txt documentation index for AI tools discovering Pinax API.',
tags: ['Documentation'],
security: [],
responses: {
200: {
description: 'Successful Response',
content: {
[contentTypeMarkdown]: {
schema: {
type: 'string',
example: `# Pinax API
> Real-time Token API, prediction market, and perp exchange data.
...`,
},
},
},
},
},
});
const openapiSpecOpenapi = describeRoute({
operationId: 'getOpenapiSpec',
summary: 'OpenAPI Specification',
description: 'Returns the OpenAPI specification for Pinax API.',
tags: ['Documentation'],
security: [],
responses: {
200: {
description: 'Successful Response',
content: {
'application/json': {
schema: {
type: 'object',
example: {
openapi: '3.1.0',
info: {},
servers: [],
paths: {},
components: {},
tags: [],
},
},
},
},
},
},
});
const x402DiscoveryOpenapi = describeRoute({
operationId: 'getX402Discovery',
summary: 'x402 Discovery',
description:
'Returns the x402 discovery document. Payment enforcement, verification, settlement, and metering are handled by the proxy layer.',
tags: ['Documentation'],
security: [],
responses: {
200: {
description: 'Successful Response',
content: {
[contentTypeJson]: {
schema: {
type: 'object',
example: {
x402Version: 2,
name: 'Pinax API',
resourceServer: 'https://api.pinax.network',
payment: {
enforcement: 'proxy',
},
discovery: {
openapi: 'https://api.pinax.network/openapi',
llms: 'https://api.pinax.network/llms.txt',
skill: 'https://api.pinax.network/SKILL.md',
},
datasets: [],
},
},
},
},
},
},
});
app.get('/SKILL.md', skillsMarkdownOpenapi, () => markdownResponse(skillsMarkdownFile));
app.get('/skills/SKILL.md', (c) => c.redirect('/SKILL.md', 301));
app.get('/SKILLS.md', (c) => c.redirect('/SKILL.md', 301));
app.get('/skills.md', (c) => c.redirect('/SKILL.md', 301));
app.get('/skill.md', (c) => c.redirect('/SKILL.md', 301));
app.get('/llms.txt', llmsTextOpenapi, () => markdownResponse('./public/llms.txt'));
app.get('/.well-known/x402', x402DiscoveryOpenapi, (c) => c.json(createX402Discovery()));
app.get('/x402.json', (c) => c.redirect('/.well-known/x402', 301));
app.get(
'/openapi',
openapiSpecOpenapi,
openAPIRouteHandler(app, {
documentation: {
info: {
title: 'Pinax API',
version: APP_VERSION,
description:
'Power your apps and AI agents with real-time Token API, prediction market, and perp exchange data.',
},
servers: config.disableOpenapiServers
? [{ url: `http://${config.hostname}:${config.port}`, description: `${APP_DESCRIPTION} - Local` }]
: [{ url: config.apiUrl, description: `${APP_DESCRIPTION} - Remote` }],
tags: [
// Documentation
{ name: 'Documentation' },
// SVM
{ name: 'SVM Tokens' },
{ name: 'SVM Tokens (Native)' },
{ name: 'SVM DEXs' },
// EVM
{ name: 'EVM Tokens (ERC-20)' },
{ name: 'EVM Tokens (Native)' },
{ name: 'EVM DEXs' },
{ name: 'EVM NFTs' },
// TVM
{ name: 'TVM Tokens (ERC-20)' },
{ name: 'TVM Tokens (Native)' },
{ name: 'TVM DEXs' },
// Polymarket
{ name: 'Polymarket Markets' },
{ name: 'Polymarket Platform' },
{ name: 'Polymarket Users' },
// Hyperliquid
{ name: 'Hyperliquid Markets' },
{ name: 'Hyperliquid Outcomes' },
{ name: 'Hyperliquid Users' },
{ name: 'Hyperliquid Vaults' },
{ name: 'Hyperliquid Platform' },
// Monitoring
{ name: 'Monitoring' },
],
components: {
securitySchemes: {
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
apiKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'X-Api-Key',
},
},
},
},
excludeStaticFile: false,
})
);
// 404 NOT FOUND
app.notFound((c: Context) =>
APIErrorResponse(c, 404, 'route_not_found', `Path not found: ${c.req.method} ${c.req.path}`)
);
logServerInit();
export default {
...app,
idleTimeout: config.idleTimeout,
};