npx skills add https://github.com/encoredev/skills --skill encore-go-api使用 Encore Go 创建 API 端点时,请遵循以下模式:
在函数上方使用 //encore:api 注解:
package user
import "context"
type GetUserParams struct {
ID string
}
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
// 实现
return &User{ID: params.ID, Email: "user@example.com", Name: "John"}, nil
}
type CreateUserParams struct {
Email string `json:"email"`
Name string `json:"name"`
}
//encore:api public method=POST path=/users
func CreateUser(ctx context.Context, params *CreateUserParams) (*User, error) {
// 实现
return &User{ID: "new-id", Email: params.Email, Name: params.Name}, nil
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 选项 | 值 | 描述 |
|---|---|---|
public | - | 可从外部访问 |
private | - | 仅可从其他服务调用 |
auth | - | 需要身份验证 |
method | GET, POST, PUT, PATCH, DELETE | HTTP 方法 |
path | string | URL 路径,使用 :param 表示路径参数 |
sensitive | - | 从跟踪记录中隐藏请求/响应负载 |
//encore:api public method=GET path=/health
//encore:api private method=POST path=/internal/process
//encore:api auth method=GET path=/profile
//encore:api public sensitive method=POST path=/auth/login
标记敏感字段以从跟踪日志中隐藏它们:
type LoginParams struct {
Email string `json:"email"`
Password string `json:"password" encore:"sensitive"`
}
或者在注解中将整个端点标记为敏感:
//encore:api public sensitive method=POST path=/auth/login
func Login(ctx context.Context, params *LoginParams) (*TokenResponse, error) {
// 请求和响应将从跟踪记录中隐藏
}
使用 encore:"httpstatus" 标签返回自定义 HTTP 状态码:
type CreateResponse struct {
ID string `json:"id"`
Status int `encore:"httpstatus"`
}
//encore:api public method=POST path=/items
func CreateItem(ctx context.Context, params *CreateParams) (*CreateResponse, error) {
item := createItem(params)
return &CreateResponse{
ID: item.ID,
Status: 201, // 返回 HTTP 201 Created
}, nil
}
// 路径:/users/:id
type GetUserParams struct {
ID string // 自动从 :id 映射
}
// 路径:/users
type ListUsersParams struct {
Limit int `query:"limit"`
Offset int `query:"offset"`
}
//encore:api public method=GET path=/users
func ListUsers(ctx context.Context, params *ListUsersParams) (*ListResponse, error) {
// params.Limit 和 params.Offset 来自查询字符串
}
type WebhookParams struct {
Signature string `header:"X-Webhook-Signature"`
Payload string `json:"payload"`
}
import "net/http"
type AuthParams struct {
SessionCookie *http.Cookie `cookie:"session"`
CSRFToken string `header:"X-CSRF-Token"`
}
//encore:api auth method=POST path=/logout
func Logout(ctx context.Context, params *AuthParams) error {
// 访问 params.SessionCookie.Value
return nil
}
使用 //encore:api raw 处理 Webhook 或直接 HTTP 访问:
import "net/http"
//encore:api public raw path=/webhooks/stripe method=POST
func StripeWebhook(w http.ResponseWriter, req *http.Request) {
sig := req.Header.Get("Stripe-Signature")
// 处理原始请求...
w.WriteHeader(http.StatusOK)
}
type Response struct {
Message string `json:"message"`
}
//encore:api public method=GET path=/hello
func Hello(ctx context.Context) (*Response, error) {
return &Response{Message: "Hello, World!"}, nil
}
//encore:api public method=DELETE path=/users/:id
func DeleteUser(ctx context.Context, params *DeleteParams) error {
// 仅返回错误(成功时无响应体)
return nil
}
//encore:api public method=GET path=/health
func Health(ctx context.Context) (*HealthResponse, error) {
return &HealthResponse{Status: "ok"}, nil
}
使用 errs 包返回正确的 HTTP 错误响应:
import "encore.dev/beta/errs"
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
user, err := findUser(params.ID)
if err != nil {
return nil, err
}
if user == nil {
return nil, &errs.Error{
Code: errs.NotFound,
Message: "user not found",
}
}
return user, nil
}
| 代码 | HTTP 状态码 | 用途 |
|---|---|---|
errs.NotFound | 404 | 资源不存在 |
errs.InvalidArgument | 400 | 输入错误 |
errs.Unauthenticated | 401 | 缺少/无效的身份验证 |
errs.PermissionDenied | 403 | 不允许 |
errs.AlreadyExists | 409 | 重复资源 |
//encore:api 注解error 作为最后一个返回值每周安装次数
125
代码仓库
GitHub 星标数
20
首次出现
2026年1月21日
安全审计
安装于
opencode105
gemini-cli104
codex104
claude-code95
github-copilot86
cursor83
When creating API endpoints with Encore Go, follow these patterns:
Use the //encore:api annotation above your function:
package user
import "context"
type GetUserParams struct {
ID string
}
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
}
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
// Implementation
return &User{ID: params.ID, Email: "user@example.com", Name: "John"}, nil
}
type CreateUserParams struct {
Email string `json:"email"`
Name string `json:"name"`
}
//encore:api public method=POST path=/users
func CreateUser(ctx context.Context, params *CreateUserParams) (*User, error) {
// Implementation
return &User{ID: "new-id", Email: params.Email, Name: params.Name}, nil
}
| Option | Values | Description |
|---|---|---|
public | - | Accessible from outside |
private | - | Only callable from other services |
auth | - | Requires authentication |
method | GET, POST, PUT, PATCH, DELETE | HTTP method |
path | string | URL path with :param for path params |
sensitive |
//encore:api public method=GET path=/health
//encore:api private method=POST path=/internal/process
//encore:api auth method=GET path=/profile
//encore:api public sensitive method=POST path=/auth/login
Mark sensitive fields to redact them from tracing logs:
type LoginParams struct {
Email string `json:"email"`
Password string `json:"password" encore:"sensitive"`
}
Or mark the entire endpoint as sensitive in the annotation:
//encore:api public sensitive method=POST path=/auth/login
func Login(ctx context.Context, params *LoginParams) (*TokenResponse, error) {
// Request and response will be redacted from traces
}
Return custom HTTP status codes using the encore:"httpstatus" tag:
type CreateResponse struct {
ID string `json:"id"`
Status int `encore:"httpstatus"`
}
//encore:api public method=POST path=/items
func CreateItem(ctx context.Context, params *CreateParams) (*CreateResponse, error) {
item := createItem(params)
return &CreateResponse{
ID: item.ID,
Status: 201, // Returns HTTP 201 Created
}, nil
}
// Path: /users/:id
type GetUserParams struct {
ID string // Automatically mapped from :id
}
// Path: /users
type ListUsersParams struct {
Limit int `query:"limit"`
Offset int `query:"offset"`
}
//encore:api public method=GET path=/users
func ListUsers(ctx context.Context, params *ListUsersParams) (*ListResponse, error) {
// params.Limit and params.Offset come from query string
}
type WebhookParams struct {
Signature string `header:"X-Webhook-Signature"`
Payload string `json:"payload"`
}
import "net/http"
type AuthParams struct {
SessionCookie *http.Cookie `cookie:"session"`
CSRFToken string `header:"X-CSRF-Token"`
}
//encore:api auth method=POST path=/logout
func Logout(ctx context.Context, params *AuthParams) error {
// Access params.SessionCookie.Value
return nil
}
Use //encore:api raw for webhooks or direct HTTP access:
import "net/http"
//encore:api public raw path=/webhooks/stripe method=POST
func StripeWebhook(w http.ResponseWriter, req *http.Request) {
sig := req.Header.Get("Stripe-Signature")
// Handle raw request...
w.WriteHeader(http.StatusOK)
}
type Response struct {
Message string `json:"message"`
}
//encore:api public method=GET path=/hello
func Hello(ctx context.Context) (*Response, error) {
return &Response{Message: "Hello, World!"}, nil
}
//encore:api public method=DELETE path=/users/:id
func DeleteUser(ctx context.Context, params *DeleteParams) error {
// Return only error (no response body on success)
return nil
}
//encore:api public method=GET path=/health
func Health(ctx context.Context) (*HealthResponse, error) {
return &HealthResponse{Status: "ok"}, nil
}
Use errs package for proper HTTP error responses:
import "encore.dev/beta/errs"
//encore:api public method=GET path=/users/:id
func GetUser(ctx context.Context, params *GetUserParams) (*User, error) {
user, err := findUser(params.ID)
if err != nil {
return nil, err
}
if user == nil {
return nil, &errs.Error{
Code: errs.NotFound,
Message: "user not found",
}
}
return user, nil
}
| Code | HTTP Status | Usage |
|---|---|---|
errs.NotFound | 404 | Resource doesn't exist |
errs.InvalidArgument | 400 | Bad input |
errs.Unauthenticated | 401 | Missing/invalid auth |
errs.PermissionDenied | 403 | Not allowed |
errs.AlreadyExists | 409 |
//encore:api annotation above the functionerror as the last return valueWeekly Installs
125
Repository
GitHub Stars
20
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode105
gemini-cli104
codex104
claude-code95
github-copilot86
cursor83
lark-cli 共享规则:飞书资源操作指南与权限配置详解
39,000 周安装
API设计模式最佳实践指南:RESTful原则、错误处理、分页与安全性
102 周安装
Groove Work Plan:AI辅助代码库分析与项目计划生成工具 | 自动化开发流程
119 周安装
Groove Git日志自动化工具 - 自动生成每日Git提交摘要和变更记录
119 周安装
自媒体自动发布工具 - 支持百家号、知乎、公众号等平台一键发布,提升内容分发效率
105 周安装
Outlook自动化指南:通过Rube MCP与Composio工具包实现邮件、日历、联系人管理
83 周安装
WhoDB数据库助手:简化数据库操作,支持SQL查询、模式探索与数据导出
93 周安装
| - |
| Redacts request/response payloads from traces |
| Duplicate resource |