MCPcopy Create free account
hub / github.com/53AI/53AIHub / CreateAPIKey

Method CreateAPIKey

api/controller/api_key.go:72–107  ·  view source on GitHub ↗

CreateAPIKey 创建API密钥 @Summary 创建API密钥 @Description 创建一个新的API密钥用于外部系统集成。仅限管理员或有知识库管理权限的用户调用。 @Tags API密钥管理 @Accept json @Produce json @Security BearerAuth @Param request body CreateAPIKeyRequest true "创建API密钥请求" @Param library_id path int false "知识库ID(通过路径传递)" @Success 200 {object} model.CommonRespon

(c *gin.Context)

Source from the content-addressed store, hash-verified

70// @Router /api/api-keys [post]
71// @Router /api/libraries/{library_id}/api-keys [post] # 知识库相关路由
72func (ctrl *APIKeyController) CreateAPIKey(c *gin.Context) {
73 var req CreateAPIKeyRequest
74 if err := c.ShouldBindJSON(&req); err != nil {
75 c.JSON(http.StatusBadRequest, model.ParamError.ToResponse(err))
76 return
77 }
78
79 eid := config.GetEID(c)
80 creatorID := config.GetUserId(c)
81 role := config.GetUserRole(c)
82
83 var libraryID *int64
84 libraryIDParam := c.Param("library_id")
85
86 if libraryIDParam != "" {
87 id, err := strconv.ParseInt(libraryIDParam, 10, 64)
88 if err != nil {
89 c.JSON(http.StatusBadRequest, model.ParamError.ToNewErrorResponse("无效的知识库ID参数"))
90 return
91 }
92 libraryID = &id
93 }
94
95 apiKeyService := mcpsvc.NewAPIKeyService()
96 apiKey, apiKeyStr, err := apiKeyService.CreateAPIKey(c.Request.Context(), eid, creatorID, role, req.Name, req.Description, libraryID)
97 if err != nil {
98 if isPermissionRelatedError(err) {
99 c.JSON(http.StatusForbidden, model.ForbiddenError.ToResponse(err))
100 return
101 }
102 c.JSON(http.StatusInternalServerError, model.DBError.ToResponse(err))
103 return
104 }
105
106 c.JSON(http.StatusOK, model.Success.ToResponse(CreateAPIKeyResponse{Key: apiKeyStr, ID: apiKey.ID}))
107}
108
109// GetAPIKeys 获取API密钥列表
110// @Summary 获取API密钥列表

Callers

nothing calls this directly

Calls 5

CreateAPIKeyMethod · 0.95
isPermissionRelatedErrorFunction · 0.85
ToResponseMethod · 0.80
GetEIDMethod · 0.80
ToNewErrorResponseMethod · 0.80

Tested by

no test coverage detected