When you're familiar with how Encore works, you can simplify your development workflow by copy-pasting these examples. If you're looking for details on how Encore works, please refer to the relevant docs section.
// PingParams is the request data for the Ping endpoint.type PingParams struct {
Name string
}
// PingResponse is the response data for the Ping endpoint.type PingResponse struct {
Message string
}
import"net/http"// Webhook receives incoming webhooks from Some Service That Sends Webhooks.//encore:api public rawfuncWebhook(w http.ResponseWriter, req *http.Request) {
// ... operate on the raw HTTP request ...
}
Hint: Like any other API endpoint, this will be exposed at: https://<env>-<app-id>.encr.app/service.Webhook
To create a database, import encore.dev/storage/sqldb and call sqldb.NewDatabase, assigning the result to a package-level variable.
sqldb.DatabaseConfig specifies the directory containing the database migration files, which is how you define the database schema.
todo/db.go
todo/migrations/1_create_table.up.sql
package todo
// Create the todo database and assign it to the "tododb" variablevar tododb = sqldb.NewDatabase("todo", sqldb.DatabaseConfig{
Migrations: "./migrations",
})
// Then, query the database using db.QueryRow, db.Exec, etc.
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
-- etc...
);
To read a single todo item in the example schema above, we can use sqldb.QueryRow:
import"encore.dev/storage/sqldb"var item struct {
ID int64
Title string
Done bool
}
err := tododb.QueryRow(ctx, `
SELECT id, title, done
FROM todo_item
LIMIT 1
`).Scan(&item.ID, &item.Title, &item.Done)
Hint: If sqldb.QueryRow does not find a matching row, it reports an error that can be checked against
by importing the standard library errors package and calling errors.Is(err, sqldb.ErrNoRows).
import"encore.dev/pubsub"type SignupEvent struct { UserID int }
var Signups = pubsub.NewTopic[*SignupEvent]("signups", pubsub.TopicConfig {
DeliveryGuarantee: pubsub.AtLeastOnce,
})
Hint: Topics are declared as package level variables and cannot be created inside functions. Regardless of where you create a topic, it can be published and subscribed to from any service.
Hint: If you want to publish to the topic from another service, import the topic package variable (Signups in this example) and call publish on it from there.
import"encore.dev/storage/cache"var MyCacheCluster = cache.NewCluster("my-cache-cluster", cache.ClusterConfig{
// EvictionPolicy tells Redis how to evict keys when the cache reaches// its memory limit. For typical cache use cases, cache.AllKeysLRU is a good default.
EvictionPolicy: cache.AllKeysLRU,
})
$ encore secret set --type <types...> <secret-name>
Hint:<types> defines which environment types the secret value applies to. Use a comma-separated list of production, development, preview, and local. For each Secret, there can only be one secret value for each environment type.
Hint: Secret keys are globally unique for your whole application; if multiple services use the same secret name they both receive the same secret value at runtime.