YouTip LogoYouTip

Skills Api Integration

Skill scripts can call external HTTP APIs like regular Python programs, thereby connecting Claude's capabilities with third-party services. * * * ## Basic Pattern for Calling External APIs Using the requests library to send HTTP requests is the most common way to call APIs in Skill scripts. ## Example # File path: scripts/api_client.py import requests import json import sys import os # API Keys are read from environment variables, not hardcoded in the script API_KEY =os.environ.get("MY_API_KEY","") BASE_URL ="https://api.example.com/v1" def call_api(endpoint: str, payload: dict, timeout: int=30) ->dict: """ Generic API call wrapper Parameters: endpoint: API Path, such as "/analyze" payload: Request body (JSON) timeout: Timeout in seconds, default 30 Returns: Parsed JSON response, or a dictionary containing error """ if not API_KEY: return{"error": "API key not set, please set the environment variable MY_API_KEY"} url = BASE_URL + endpoint headers ={ "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } try: response = requests.post(url, headers=headers, json=payload, timeout=timeout) response.raise_for_status()# HTTP 4xx/5xx Throw exception return response.json() except requests.Timeout: return{"error": f"Request timeout (exceeding {timeout} SecondοΌ‰"} except requests.HTTPError as e: return{"error": f"HTTP Errors {e.response.status_code}: {e.response.text}"} except requests.ConnectionError: return{"error": "Unable to connect to API service, please check network"} except Exception as e: return{"error": str(e)} # Usage example if __name__ =="__main__": result = call_api("/analyze",{ "text": "tutorial is a well-known domestic technology learning platform", "lang": "zh" }) print(json.dumps(result, ensure_ascii=False, indent=2)) * * * ## API Key Security Management API keys should not be written directly in script files; they should be passed through environment variables. ## Example # Set environment variables before running the script export MY_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxx" python scripts/api_client.py # Or set inline during script execution MY_API_KEY="sk-xxxxxxxxxxxxxxxxxxxxxxxx" python scripts/api_client.py In SKILL.md, inform users that they need to configure the key: ## Prerequisites This Skill requires an external API key. Before first use, please complete the following configuration: 1. Register on the and obtain an API key 2. Set the environment variable in the terminal: ```bash export MY_API_KEY="your-key" 3. To make it permanent, add the above command to ~/.bashrc or ~/.zshrc If not configured, the Skill will prompt "API key not set" and exit. > Never commit API keys to Git repositories. You can create a .gitignore file in the Skill directory to exclude .env files from version control. * * * ## Handling API Rate Limiting Most APIs have request frequency limits. When batch calling APIs, you need to add delays between requests. ## Example # File path: scripts/rate_limited_client.py import time import requests class RateLimitedClient: """API client with rate limiting""" def __init__ (self, base_url: str, api_key: str, requests_per_minute: int=60): self.base_url= base_url self.api_key= api_key self.min_interval=60.0 ```
← Skills SecuritySkills Description Optimize β†’