diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index 930f945ba9..f82c84c1e3 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -4975,7 +4975,9 @@ def hide_discussion_comment( return deserialize_event(resp.json()["updatedComment"]) # type: ignore @validate_hf_hub_args - def add_space_secret(self, repo_id: str, key: str, value: str, *, token: Optional[str] = None) -> None: + def add_space_secret( + self, repo_id: str, key: str, value: str, *, description: Optional[str] = None, token: Optional[str] = None + ) -> None: """Adds or updates a secret in a Space. Secrets allow to set secret keys or tokens to a Space without hardcoding them. @@ -4988,13 +4990,18 @@ def add_space_secret(self, repo_id: str, key: str, value: str, *, token: Optiona Secret key. Example: `"GITHUB_API_KEY"` value (`str`): Secret value. Example: `"your_github_api_key"`. + description (`str`, *optional*): + Secret description. Example: `"Github API key to access the Github API"`. token (`str`, *optional*): Hugging Face token. Will default to the locally saved token if not provided. """ + payload = {"key": key, "value": value} + if description is not None: + payload["description"] = description r = get_session().post( f"{self.endpoint}/api/spaces/{repo_id}/secrets", headers=self._build_hf_headers(token=token), - json={"key": key, "value": value}, + json=payload, ) hf_raise_for_status(r) diff --git a/tests/test_hf_api.py b/tests/test_hf_api.py index bf93989617..7efab17300 100644 --- a/tests/test_hf_api.py +++ b/tests/test_hf_api.py @@ -2399,9 +2399,16 @@ def test_manage_secrets(self) -> None: self.api.add_space_secret(self.repo_id, "token", "hf_api_123456") self.api.add_space_secret(self.repo_id, "gh_api_key", "******") + # Add secret with optional description + self.api.add_space_secret(self.repo_id, "bar", "123", description="This is a secret") + # Update secret self.api.add_space_secret(self.repo_id, "foo", "456") + # Update secret with optional description + self.api.add_space_secret(self.repo_id, "foo", "789", description="This is a secret") + self.api.add_space_secret(self.repo_id, "bar", "456", description="This is another secret") + # Delete secret self.api.delete_space_secret(self.repo_id, "gh_api_key")