-
Notifications
You must be signed in to change notification settings - Fork 615
Expand file tree
/
Copy pathts_llm_instructions.txt
More file actions
1139 lines (897 loc) · 38.3 KB
/
Copy pathts_llm_instructions.txt
File metadata and controls
1139 lines (897 loc) · 38.3 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## Node.js style guide
MUST write valid TypeScript w/ Node.js v20+ features + best practices:
- ES6+ syntax always.
- Built-in `fetch` for HTTP, not `node-fetch`.
- Node.js `import`, never `require`.
## TypeScript style guide
- interface/type for complex objects
- Built-in utility types (Record, Partial, Pick) over any
## Generated folders — do not edit
`encore.gen/` + `.encore/` = CLI-regenerated; never edit.
`encore.app` at repo root = app manifest — JSON **text** file, not binary despite `.app` ext. Read like any source file.
## Encore local MCP server
Local Encore MCP wired via `.mcp.json` (registered as **`encore-local`**, tool prefix `mcp__encore-local__`). Prefer for live app introspection over `Read`/grep; use docs tools when unsure of Encore API surface.
### Tools
20 tools, all prefixed `mcp__encore-local__`. MCP client gets full catalog (names, descriptions, schemas) via `tools/list` on session init — no list here.
### Important tools (runtime-only — no filesystem equivalent)
These do what `Glob`/`Grep`/`Read` can't. For static config (services, endpoints, topics, schemas), see "When NOT to use it" below.
- **`call_endpoint`** — args: `service`, `endpoint`, `method`, `path`, `payload` (JSON string w/ body/query/headers/path-params), optional `auth_token` / `auth_payload` / `correlation_id`. Optional `retry_until: { predicate, timeout_ms?, interval_ms?, fail_on_timeout? }` polls **server-side** until predicate matches — predicate = `status: 200` OR `body_path: { path: ".events.0.orderID", equals: 7 }` OR `body_jq: ".events | length > 0"` (minimal subset: `<path> | length <op> N` or `<path>` truthy). Use instead of agent-side polling loops for eventually-consistent reads. **auto-starts app** if not running. Use instead of `curl` to test endpoints.
- **`wait_for_subscription_message`** — args: `topic`, optional `subscription`, `timeout_ms` (default 10000), `since` (ISO/RFC3339), `match` (top-level key/value JSON filter, e.g. `{"customerID":"cust_42"}`). Blocks until the next message on the topic is fully processed by a subscription handler (returns or errors), then returns `{outcome, payload, duration_ms, handler_error, trace_id}`. Use to bridge async Pub/Sub work into a synchronous verify step — beats polling read-side endpoints + introspecting via `get_pubsub`/`get_traces`.
- **`query_database`** — args: `queries` (array of `{database, query}`). Runs SQL against named DBs, multi queries per call. Beats `encore db conn-uri` + `psql` round-trips.
- **`get_traces`** — args: optional `service`, `endpoint`, `topic`, `subscription`, `error` (`"true"`/`"false"`), `start_time` / `end_time` (ISO), `min_duration_ms` / `max_duration_ms`, `parent_trace_id`, `limit`. Recent root request/Pub/Sub/test traces (timing, status, ids).
- **`get_trace_spans`** — args: `trace_ids` (array from `get_traces`). Full per-span details for deep debug.
- **`get_objects`** — args: `buckets` (array of bucket names). Lists objects + metadata in storage buckets.
- **`search_docs`** — args: `query`, optional `hits_per_page`, `page`, `facet_filters`. Algolia-backed Encore docs search.
- **`get_docs`** — args: `paths` (array of doc paths, e.g. `/docs/ts/primitives/databases`). Fetches full doc pages found via `search_docs`.
### When to reach for it
| Situation | Tool |
|---|---|
| "What services / endpoints / databases exist?" | `get_metadata` once |
| "Test my new POST /orders endpoint" | `call_endpoint` (auto-starts app) |
| "Did the Pub/Sub handler run after I published?" | `wait_for_subscription_message` |
| "Endpoint output is eventually consistent (e.g. read-after-publish)" | `call_endpoint` w/ `retry_until` |
| "Why did last request fail?" | `get_traces` then `get_trace_spans` |
| "How does Encore API surface work?" | `search_docs` then `get_docs` |
- **MCP only for runtime data** - For **static structure** in source (services, endpoints, topics, subscriptions, schemas, secrets, middleware, cron jobs), `Glob`+`Grep`+`Read` = faster + more reliable than `get_*` tool.
### Cloud MCP (deployed environments)
If deployed to Encore Cloud, also register `encore-cloud` via `claude mcp add --transport http encore-cloud https://api.encore.dev/mcp` for prod traces / deploy state — `encore-local` only sees local app.
## Encore check (verify app builds + endpoints work)
`encore check` compiles, boots, health-checks app, optionally runs `curl` cmds once healthy. Use instead of manual `encore run + healthz poll + curl` pattern.
```bash
encore check # compile + boot only
encore check 'curl /ping' # GET a relative path
encore check 'curl /orders -X POST -d "{\"customer_id\":\"c1\",\"amount_cents\":1999}"' # POST with JSON body (flags after path)
encore check 'curl /a; curl /b' # chain in one quoted string
encore check 'curl /a' 'curl /b' # or as separate quoted args
encore check < commands.txt # one curl per line, piped from file
```
Rules for embedded curl DSL:
- **Paths MUST be relative** (start w/ `/`). Use `curl /orders/1`, **not** `curl http://localhost:4000/orders/1` — host/port supplied by `encore check`. Absolute URL fails w/ `curl path "http..." must be relative`.
- **Path first, flags after.** `curl /orders -X POST -d '...'` works; `curl -X POST /orders ...` fails — parser reads first token after `curl` as path, rejects flags (`curl path "-X" must be relative`).
- One curl per quoted cmd. Chain w/ `;` inside one quoted string, pass multi quoted args, or pipe file w/ one curl per line.
## Pub/sub verification
- Delivery = async + at-least-once + not order-preserving.
- For "most-recent first" by publish order, do NOT sort by `created_at = NOW()` nor subscription-side row id — both = subscription-insert order, at-least-once can flip. Sort by column monotonic w/ publish (entity id from event payload, or publish-time seq in event).
- For handler-only checks, skip infra: `et.Topic(T).PublishedMessages()`.
## Encore.ts domain knowledge
### API definition
Encore.ts = type-safe TS API endpoints w/ built-in request validation. APIs = async fns w/ TS interfaces for req/resp. Source parsing enables auto req validation against schemas.
Syntax:
import { api } from "encore.dev/api";
export const endpoint = api(options, async handler);
Options: method (HTTP method), expose (bool public access, default: false), auth (bool requires auth), path (URL path pattern)
Example:
import { api } from "encore.dev/api";
interface PingParams {
name: string;
}
interface PingResponse {
message: string;
}
export const ping = api(
{ method: "POST" },
async (p: PingParams): Promise<PingResponse> => {
return { message: Hello ${p.name}! };
}
);
Schema patterns:
- Full: api({ ... }, async (params: Params): Promise<Response> => {})
- Resp only: api({ ... }, async (): Promise<Response> => {})
- Req only: api({ ... }, async (params: Params): Promise<void> => {})
- No data: api({ ... }, async (): Promise<void> => {})
Param types:
- Header<"Header-Name">: maps field to HTTP header
- Query<type>: maps field to URL query param
- Path: maps to URL path params via :param or *wildcard
### API calls
Service-to-service calls = simple fn call syntax. Services imported from ~encore/clients. Compile-time type check + IDE autocomplete.
Example:
import { hello } from "~encore/clients";
export const myOtherAPI = api({}, async (): Promise<void> => {
const resp = await hello.ping({ name: "World" });
console.log(resp.message); // "Hello World!"
});
### Application structure
Core:
- Monorepo design for entire backend
- One Encore app = full app model benefits
- Supports monolith + microservices
- Services cannot nest
Service def:
import { Service } from "encore.dev/service";
export default new Service("my-service");
Patterns:
Single service (best start):
/my-app
├── package.json
├── encore.app
├── encore.service.ts // service root
├── api.ts // endpoints
└── db.ts // database
Multi service:
/my-app
├── encore.app
├── hello/
│ ├── migrations/
│ ├── encore.service.ts
│ ├── hello.ts
│ └── hello_test.ts
└── world/
├── encore.service.ts
└── world.ts
Large scale (systems-based):
/my-trello-clone
├── encore.app
├── trello/ // system
│ ├── board/ // service
│ └── card/ // service
├── premium/ // system
│ ├── payment/ // service
│ └── subscription/ // service
└── usr/ // system
├── org/ // service
└── user/ // service
### Raw endpoints
Raw endpoints = lower-level HTTP req access via Node.js/Express.js style handling. For webhooks + custom HTTP handling.
Example:
import { api } from "encore.dev/api";
export const myRawEndpoint = api.raw(
{ expose: true, path: "/raw", method: "GET" },
async (req, resp) => {
resp.writeHead(200, { "Content-Type": "text/plain" });
resp.end("Hello, raw world!");
}
);
Usage: curl http://localhost:4000/raw → Hello, raw world!
Use: webhooks, custom HTTP resp format, direct req/resp control
### API errors
Error format:
{
"code": "not_found",
"message": "sprocket not found",
"details": null
}
Impl:
import { APIError, ErrCode } from "encore.dev/api";
throw new APIError(ErrCode.NotFound, "sprocket not found");
// shorthand:
throw APIError.notFound("sprocket not found");
Error codes (name → string_value → HTTP status):
- OK → ok → 200 OK
- Canceled → canceled → 499 Client Closed Request
- Unknown → unknown → 500 Internal Server Error
- InvalidArgument → invalid_argument → 400 Bad Request
- DeadlineExceeded → deadline_exceeded → 504 Gateway Timeout
- NotFound → not_found → 404 Not Found
- AlreadyExists → already_exists → 409 Conflict
- PermissionDenied → permission_denied → 403 Forbidden
- ResourceExhausted → resource_exhausted → 429 Too Many Requests
- FailedPrecondition → failed_precondition → 400 Bad Request
- Aborted → aborted → 409 Conflict
- OutOfRange → out_of_range → 400 Bad Request
- Unimplemented → unimplemented → 501 Not Implemented
- Internal → internal → 500 Internal Server Error
- Unavailable → unavailable → 503 Unavailable
- DataLoss → data_loss → 500 Internal Server Error
- Unauthenticated → unauthenticated → 401 Unauthorized
withDetails on APIError = attach structured details returned to external clients.
### SQL databases
Encore treats SQL DBs as logical resources + natively supports PostgreSQL.
DB creation:
import { SQLDatabase } from "encore.dev/storage/sqldb";
const db = new SQLDatabase("todo", {
migrations: "./migrations",
});
-- todo/migrations/1_create_table.up.sql --
CREATE TABLE todo_item (
id BIGSERIAL PRIMARY KEY,
title TEXT NOT NULL,
done BOOLEAN NOT NULL DEFAULT false
);
Migration naming: starts w/ number + underscore, must increase sequentially, ends w/ .up.sql (e.g., 001_first_migration.up.sql, 002_second_migration.up.sql)
DB ops (use only these methods):
- query: async iterator for multi rows
const allTodos = await db.query`SELECT * FROM todo_item`;
for await (const todo of allTodos) {
// Process each todo
}
**Always parameterize `db.query` / `db.queryRow` w/ explicit row generic.** Without it `row.*` accesses typed `any` + silently drop into resp (e.g. `TIMESTAMPTZ` col lands as `Date` in `string`-typed resp field, no compile error). Row type fields = snake_case SQL cols, not camelCase API resp — map between yourself in loop.
const rows = await db.query<{ email: string; source_url: string; scraped_at: Date }>`
SELECT email, source_url, created_at as scraped_at
FROM scraped_emails
ORDER BY created_at DESC
`;
const emails = [];
for await (const row of rows) {
emails.push(row);
}
return { emails };
- queryRow: single row or null
async function getTodoTitle(id: number): string | undefined {
const row = await db.queryRow`SELECT title FROM todo_item WHERE id = ${id}`;
return row?.title;
}
- exec: inserts + queries not returning rows
await db.exec`
INSERT INTO todo_item (title, done)
VALUES (${title}, false)
`;
CLI: db shell (opens psql), db conn-uri (outputs conn string), db proxy (local conn proxy)
Advanced:
- Sharing DBs: export SQLDatabase from shared module or use SQLDatabase.named("name") to ref existing DB
- Extensions: pgvector, PostGIS (uses encoredotdev/postgres Docker image)
- ORM: Prisma, Drizzle (must support std SQL driver + gen std SQL files)
### Cron jobs
Encore.ts = declarative Cron Jobs for periodic + recurring tasks.
Example:
import { CronJob } from "encore.dev/cron";
import { api } from "encore.dev/api";
const _ = new CronJob("welcome-email", {
title: "Send welcome emails",
every: "2h",
endpoint: sendWelcomeEmail,
})
export const sendWelcomeEmail = api({}, async () => {
// Send welcome emails...
});
Schedule:
- every: periodic, starts midnight UTC. Interval must divide 24h evenly. Valid: 10m, 6h. Invalid: 7h
- schedule: cron exprs for complex (e.g., "0 4 15 * *" = 4am UTC on 15th)
### Pub/Sub
Async event broadcasting between services. Decouples services for reliability, improves responsiveness, cloud-agnostic.
Topics (must be package-level vars, cannot create in fns, accessible from any service):
import { Topic } from "encore.dev/pubsub"
export interface SignupEvent {
userID: string;
}
export const signups = new Topic<SignupEvent>("signups", {
deliveryGuarantee: "at-least-once",
});
Publish:
const messageID = await signups.publish({userID: id});
Subscriptions:
import { Subscription } from "encore.dev/pubsub";
const _ = new Subscription(signups, "send-welcome-email", {
handler: async (event) => {
// Send a welcome email using the event.
},
});
Errors: failed events retried per retry policy. After max retries → dead-letter queue.
Delivery guarantees:
- at-least-once: default, possible dup, handlers must be idempotent
- exactly-once: stronger, minimized dups. Limits: AWS 300 msg/s/topic, GCP 3000+ msg/s/region. No publish-side dedup.
Message attributes (key-value for filter/order):
import { Topic, Attribute } from "encore.dev/pubsub";
export interface SignupEvent {
userID: string;
source: Attribute<string>;
}
Ordered delivery (delivered in order by orderingAttribute):
import { Topic, Attribute } from "encore.dev/pubsub";
export interface CartEvent {
shoppingCartID: Attribute<number>;
event: string;
}
export const cartEvents = new Topic<CartEvent>("cart-events", {
deliveryGuarantee: "at-least-once",
orderingAttribute: "shoppingCartID",
})
Limits: AWS 300 msg/s/topic, GCP 1 MBps/ordering key. No effect locally.
### Object storage
Simple + scalable file/unstructured data store.
Buckets (must be package-level vars, cannot create in fns, accessible from any service):
import { Bucket } from "encore.dev/storage/objects";
export const profilePictures = new Bucket("profile-pictures", {
versioned: false
});
Ops:
- Upload:
const data = Buffer.from(...); // image data
const attributes = await profilePictures.upload("my-image.jpeg", data, {
contentType: "image/jpeg",
});
- Download:
const data = await profilePictures.download("my-image.jpeg");
- List:
for await (const entry of profilePictures.list({})) {
// Process entry
}
- Delete:
await profilePictures.remove("my-image.jpeg");
- Attrs:
const attrs = await profilePictures.attrs("my-image.jpeg");
const exists = await profilePictures.exists("my-image.jpeg");
Public access:
export const publicProfilePictures = new Bucket("public-profile-pictures", {
public: true,
versioned: false
});
const url = publicProfilePictures.publicUrl("my-image.jpeg");
Errors: ObjectNotFound (no such object), PreconditionFailed (upload preconditions failed), ObjectsError (base)
Bucket refs (controlled access):
Permissions: Downloader, Uploader, Lister, Attrser, Remover, ReadWriter
import { Uploader } from "encore.dev/storage/objects";
const ref = profilePictures.ref<Uploader>();
Must call from within service for proper permission tracking.
### Caching
Type-safe cache layer backed by Redis w/ auto infra provisioning.
Cluster def:
import { CacheCluster } from "encore.dev/storage/cache";
const cluster = new CacheCluster("my-cache", {
evictionPolicy: "allkeys-lru",
});
Ref existing cluster from another service: const cluster = CacheCluster.named("my-cache");
Eviction policies: "allkeys-lru" (default), "noeviction", "allkeys-lfu", "allkeys-random", "volatile-lru", "volatile-lfu", "volatile-ttl", "volatile-random"
Keyspaces (type-safe storage, Key type + keyPattern → Redis keys):
StringKeyspace example:
import { CacheCluster, StringKeyspace, expireIn } from "encore.dev/storage/cache";
const cluster = new CacheCluster("my-cache", { evictionPolicy: "allkeys-lru" });
const tokens = new StringKeyspace<{ tokenId: string }>(cluster, {
keyPattern: "token/:tokenId",
defaultExpiry: expireIn(3600 * 1000),
});
await tokens.set({ tokenId: "abc123" }, "value");
const token = await tokens.get({ tokenId: "abc123" }); // undefined on miss
await tokens.delete({ tokenId: "abc123" });
IntKeyspace example:
import { IntKeyspace } from "encore.dev/storage/cache";
const counters = new IntKeyspace<{ id: string }>(cluster, {
keyPattern: "counter/:id",
});
await counters.increment({ id: "visits" }, 1);
await counters.decrement({ id: "visits" }, 1);
StructKeyspace example:
import { StructKeyspace } from "encore.dev/storage/cache";
interface UserProfile { name: string; email: string; }
const profiles = new StructKeyspace<{ userId: string }, UserProfile>(cluster, {
keyPattern: "profile/:userId",
defaultExpiry: expireIn(3600 * 1000),
});
await profiles.set({ userId: "user123" }, { name: "Alice", email: "alice@example.com" });
Other keyspaces (all from "encore.dev/storage/cache"):
- FloatKeyspace<Key>: float vals. Has increment().
- StringListKeyspace<Key>: string lists. pushRight, pushLeft, popRight, popLeft, getRange, items.
- NumberListKeyspace<Key>: number lists. pushRight, pushLeft, popRight, popLeft, items.
- StringSetKeyspace<Key>: string sets. add, remove, contains, items.
- NumberSetKeyspace<Key>: number sets. add, remove, contains, items.
Expiry helpers (from "encore.dev/storage/cache"): expireIn(ms), expireInSeconds(s), expireInMinutes(m), expireInHours(h), expireDailyAt(hour, min, sec), neverExpire, keepTTL
Write opts:
await keyspace.set(key, value, { expiry: expireInMinutes(30) }); // override default
await keyspace.setIfNotExists(key, value); // only if not exists
await keyspace.replace(key, value); // only if exists, throws CacheMiss if absent
Error types: CacheMiss, CacheKeyExists (from "encore.dev/storage/cache"). get() → undefined on miss.
Local dev = in-memory Redis impl w/ ~100 key limit.
### Secrets management
Built-in secrets mgr for API keys, passwords, private keys.
Def:
import { secret } from "encore.dev/config";
const githubToken = secret("GitHubAPIToken");
Usage:
async function callGitHub() {
const resp = await fetch("https:///api.github.com/user", {
credentials: "include",
headers: {
Authorization: `token ${githubToken()}`,
},
});
}
Secret keys globally unique across app.
Storage methods:
- Cloud dashboard: https://app.encore.dev → Settings → Secrets
- CLI: encore secret set --type <types> <secret-name> (types: production/prod, development/dev, preview/pr, local)
- Local override: .secrets.local.cue file (e.g., GitHubAPIToken: "my-local-override-token")
Env settings: one secret value per env type. Env-specific overrides env-type values.
### Streaming APIs
API endpoints for data streaming via WebSocket.
Stream types: StreamIn (client→server), StreamOut (server→client), StreamInOut (bidirectional)
StreamIn:
import { api } from "encore.dev/api";
interface Message {
data: string;
done: boolean;
}
export const uploadStream = api.streamIn<Message>(
{ path: "/upload", expose: true },
async (stream) => {
for await (const data of stream) {
// Process incoming data
if (data.done) break;
}
}
);
StreamOut:
export const dataStream = api.streamOut<Message>(
{ path: "/stream", expose: true },
async (stream) => {
// Send messages to client
await stream.send({ data: "message" });
await stream.close();
}
);
StreamInOut:
export const chatStream = api.streamInOut<InMessage, OutMessage>(
{ path: "/chat", expose: true },
async (stream) => {
for await (const msg of stream) {
await stream.send(/* response */);
}
}
);
Handshake supports: path params, query params, headers, auth data
Client usage:
const stream = client.serviceName.endpointName();
await stream.send({ /* message */ });
for await (const msg of stream) {
// Handle incoming messages
}
Service-to-service:
import { service } from "~encore/clients";
const stream = await service.streamEndpoint();
### Validation
Built-in req validation via TS types — runtime + compile-time.
Example:
import { Header, Query, api } from "encore.dev/api";
import { Min, Max, MinLen, MaxLen, IsEmail, IsURL, StartsWith, EndsWith, MatchesRegexp } from "encore.dev/validate";
interface Request {
limit?: Query<number>; // Optional query parameter
myHeader: Header<"X-My-Header">; // Required header
type: "sprocket" | "widget"; // Required enum in body
}
export const myEndpoint = api<Request, Response>(
{ expose: true, method: "POST", path: "/api" },
async ({ limit, myHeader, type }) => {
// Implementation
}
);
Basic types: string, number, boolean, arrays (string[], number[], { name: string }[], (string | number)[]), enums ("BLOG_POST" | "COMMENT")
Modifiers:
- Optional: fieldName?: type;
- Nullable: fieldName: type | null;
Rules:
- Min/Max: count: number & (Min<3> & Max<1000>);
- MinLen/MaxLen: username: string & (MinLen<5> & MaxLen<20>);
- Format: contact: string & (IsURL | IsEmail);
Source types:
- Body: default for methods w/ req bodies, parsed from JSON body
- Query: URL query params, use Query type or default for GET/HEAD/DELETE
- Headers: HTTP headers, use Header<"Name-Of-Header"> type
- Params: URL path params (e.g., path: "/user/:id", param: { id: string })
Error resp (400 Bad Request):
{
"code": "invalid_argument",
"message": "unable to decode request body",
"internal_message": "Error details"
}
### Static assets
Built-in serving for static assets (images, HTML, CSS, JS). For static sites or pre-compiled SPAs.
Basic:
import { api } from "encore.dev/api";
export const assets = api.static(
{ expose: true, path: "/frontend/*path", dir: "./assets" },
);
Serves files from ./assets under /frontend prefix. Auto-serves index.html at dir roots.
Root serving (uses !path to avoid conflicts):
export const assets = api.static(
{ expose: true, path: "/!path", dir: "./assets" },
);
Custom 404:
export const assets = api.static(
{
expose: true,
path: "/!path",
dir: "./assets",
notFound: "./not_found.html"
},
);
### GraphQL
Encore.ts has GraphQL via raw endpoints w/ auto tracing.
Apollo example:
import { HeaderMap } from "@apollo/server";
import { api } from "encore.dev/api";
const { ApolloServer, gql } = require("apollo-server");
import { json } from "node:stream/consumers";
const server = new ApolloServer({ typeDefs, resolvers });
await server.start();
export const graphqlAPI = api.raw(
{ expose: true, path: "/graphql", method: "*" },
async (req, res) => {
server.assertStarted("/graphql");
const headers = new HeaderMap();
for (const [key, value] of Object.entries(req.headers)) {
if (value !== undefined) {
headers.set(key, Array.isArray(value) ? value.join(", ") : value);
}
}
const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({
httpGraphQLRequest: {
headers,
method: req.method!.toUpperCase(),
body: await json(req),
search: new URLSearchParams(req.url ?? "").toString(),
},
context: async () => ({ req, res }),
});
// Set response headers and status
for (const [key, value] of httpGraphQLResponse.headers) {
res.setHeader(key, value);
}
res.statusCode = httpGraphQLResponse.status || 200;
// Write response
if (httpGraphQLResponse.body.kind === "complete") {
res.end(httpGraphQLResponse.body.string);
return;
}
for await (const chunk of httpGraphQLResponse.body.asyncIterator) {
res.write(chunk);
}
res.end();
}
);
REST integration example:
Schema:
type Query {
books: [Book]
}
type Book {
title: String!
author: String!
}
Resolver:
import { book } from "~encore/clients";
import { QueryResolvers } from "../__generated__/resolvers-types";
const queries: QueryResolvers = {
books: async () => {
const { books } = await book.list();
return books;
},
};
REST endpoint:
import { api } from "encore.dev/api";
import { Book } from "../__generated__/resolvers-types";
export const list = api(
{ expose: true, method: "GET", path: "/books" },
async (): Promise<{ books: Book[] }> => {
return { books: db };
}
);
### Authentication
Auth system for identifying API callers. Enable: set auth: true in endpoint options.
Auth handler:
import { Header, Gateway } from "encore.dev/api";
import { authHandler } from "encore.dev/auth";
interface AuthParams {
authorization: Header<"Authorization">;
}
interface AuthData {
userID: string;
}
export const auth = authHandler<AuthParams, AuthData>(
async (params) => {
// Authenticate user based on params
return {userID: "my-user-id"};
}
)
export const gateway = new Gateway({
authHandler: auth,
})
Reject: throw APIError.unauthenticated("bad credentials");
Auth process:
1. Determine auth: triggers on any req w/ auth params. Returns AuthData (success), throws Unauthenticated (treated as no auth), or throws other error (req aborted).
2. Endpoint call: if endpoint needs auth + req unauth → reject. If authed, auth data passed regardless of requirements.
Auth data access: import getAuthData from ~encore/auth for type-safe resolution. Auto propagates in internal API calls. Calls to auth-required endpoints fail if orig req lacks auth.
### Metadata
Access env + app info via metadata API from encore.dev.
appMeta() returns: appId (app name), apiBaseUrl (public API URL), environment (current env), build (VCS revision), deploy (deploy ID + timestamp)
currentRequest() returns:
- API call:
interface APICallMeta {
type: "api-call";
api: APIDesc;
method: Method;
path: string;
pathAndQuery: string;
pathParams: Record<string, any>;
headers: Record<string, string | string[]>;
parsedPayload?: Record<string, any>;
}
- PubSub:
interface PubSubMessageMeta {
type: "pubsub-message";
service: string;
topic: string;
subscription: string;
messageId: string;
deliveryAttempt: number;
parsedPayload?: Record<string, any>;
}
Returns undefined if called during service init.
Use cases:
import { appMeta } from "encore.dev";
// Cloud-specific behavior
async function audit(userID: string, event: Record<string, any>) {
const cloud = appMeta().environment.cloud;
switch (cloud) {
case "aws": return writeIntoRedshift(userID, event);
case "gcp": return writeIntoBigQuery(userID, event);
case "local": return writeIntoFile(userID, event);
default: throw new Error(`unknown cloud: ${cloud}`);
}
}
// Environment-specific behavior
switch (appMeta().environment.type) {
case "test":
case "development":
await markEmailVerified(userID);
break;
default:
await sendVerificationEmail(userID);
break;
}
### Middleware
Reusable code running before/after API reqs across endpoints.
Basic:
import { middleware } from "encore.dev/api";
export default new Service("myService", {
middlewares: [
middleware({ target: { auth: true } }, async (req, next) => {
// Pre-handler logic
const resp = await next(req);
// Post-handler logic
return resp
})
]
});
Req access types:
- Typed API: req.requestMeta
- Streaming: req.requestMeta, req.stream
- Raw: req.rawRequest, req.rawResponse
Resp handling (HandlerResponse):
resp.header.set(key, value)
resp.header.add(key, value)
Ordering (executes in definition order):
export default new Service("myService", {
middlewares: [
first,
second,
third
],
});
Targeting: use target option instead of runtime filter for perf. Defaults to all endpoints if target unset.
### ORM integration
Built-in ORM + migration framework support via named DBs + SQL migration files.
Requires: ORM must support std SQL driver. Migration framework must gen std SQL files.
DB conn:
import { SQLDatabase } from "encore.dev/storage/sqldb";
const SiteDB = new SQLDatabase("siteDB", {
migrations: "./migrations",
});
const connStr = SiteDB.connectionString;
### Drizzle integration
DB setup (database.ts):
import { api } from "encore.dev/api";
import { SQLDatabase } from "encore.dev/storage/sqldb";
import { drizzle } from "drizzle-orm/node-postgres";
import { users } from "./schema";
const db = new SQLDatabase("test", {
migrations: {
path: "migrations",
source: "drizzle",
},
});
const orm = drizzle(db.connectionString);
await orm.select().from(users);
Drizzle config (drizzle.config.ts):
import 'dotenv/config';
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
out: 'migrations',
schema: 'schema.ts',
dialect: 'postgresql',
});
Schema (schema.ts):
import * as p from "drizzle-orm/pg-core";
export const users = p.pgTable("users", {
id: p.serial().primaryKey(),
name: p.text(),
email: p.text().unique(),
});
Gen migrations: drizzle-kit generate (run in dir w/ drizzle.config.ts)
Migrations auto-applied at Encore runtime.
### CORS
CORS controls which origins can access API. Scope: browser reqs to resources on different origins (scheme, domain, port).
Config in encore.app under global_cors:
- debug: bool, CORS debug logging
- allow_headers: string[], additional accepted headers ("*" = all)
- expose_headers: string[], additional exposed headers ("*" = all)
- allow_origins_without_credentials: string[], allowed origins for non-cred reqs (default: ["*"])
- allow_origins_with_credentials: string[], allowed origins for credentialed (wildcards: https://*.example.com, https://*-myapp.example.com)
Defaults: allow unauth reqs from all origins, disallow auth reqs from other origins, all origins allowed in local dev.
Header handling: Encore auto-configures headers via static analysis. Additional headers configurable via allow_headers + expose_headers for custom headers in raw endpoints.
### Logging
Built-in structured logging — free-form msgs + type-safe key-value pairs.
import log from "encore.dev/log";
Levels: error, warn, info, debug, trace
Basic:
log.info("log message", {is_subscriber: true})
log.error(err, "something went terribly wrong!")
With context (group logs w/ shared key-value pairs):
const logger = log.with({is_subscriber: true})
logger.info("user logged in", {login_method: "oauth"}) // includes is_subscriber=true
### Testing
Encore.ts uses std TS testing tools. Recommended: Vitest.
Setup:
npm install -D vitest
Add to package.json:
{
"scripts": {
"test": "vitest"
}
}
Run tests:
- encore test: recommended — auto sets up test DBs, isolated infra per test, handles service deps
- npm test: direct, no infra setup
Test API endpoint:
import { describe, it, expect } from "vitest";
import { hello } from "./api";
describe("hello endpoint", () => {
it("returns a greeting", async () => {
const response = await hello();
expect(response.message).toBe("Hello, World!");
});
});
Test w/ req params:
import { describe, it, expect } from "vitest";
import { getUser } from "./api";
describe("getUser endpoint", () => {
it("returns the user by ID", async () => {
const user = await getUser({ id: "123" });
expect(user.id).toBe("123");
expect(user.name).toBeDefined();
});
});
Test DB ops (Encore gives isolated test DBs):
import { describe, it, expect, beforeEach } from "vitest";
import { createUser, getUser, db } from "./user";
describe("user operations", () => {
beforeEach(async () => {
await db.exec`DELETE FROM users`;
});
it("creates and retrieves a user", async () => {
const created = await createUser({ email: "test@example.com", name: "Test" });
const retrieved = await getUser({ id: created.id });
expect(retrieved.email).toBe("test@example.com");
});
});
Test error cases:
import { describe, it, expect } from "vitest";
import { getUser } from "./api";
import { APIError } from "encore.dev/api";
describe("error handling", () => {
it("throws NotFound for missing user", async () => {
await expect(getUser({ id: "nonexistent" }))
.rejects
.toThrow("user not found");
});
it("throws with correct error code", async () => {
try {
await getUser({ id: "nonexistent" });
} catch (error) {
expect(error).toBeInstanceOf(APIError);
expect((error as APIError).code).toBe("not_found");
}
});
});
Test Pub/Sub:
import { describe, it, expect } from "vitest";