如何开发需在URL中集成API密钥的API?ActiveCampaign API调用求助
Based on your description, it looks like you’re working with ActiveCampaign’s older v1 API (given the ?call=contact_list parameter format), but I’ll cover both that approach (to match your example) and the recommended v3 API (the current supported version) to give you the best options.
For the v1 API (Matching Your Example Endpoint)
ActiveCampaign’s v1 API requires two mandatory authentication parameters in the query string, plus the call parameter to specify the action you want to perform:
api_key: Your unique API key from your ActiveCampaign account settingsapi_url: The base URL you retrieved (https://url.api-us1.com)call: The action to execute (in your case,contact_list)
Corrected Request Code
Here’s how to adjust your existing requests code to include all required parameters:
import requests # Replace with your actual API credentials api_key = "YOUR_ACTIVECAMPAIGN_API_KEY" base_url = "https://url.api-us1.com" # Construct the full request URL and parameters request_url = f"{base_url}/api/example.php" params = { "call": "contact_list", "api_key": api_key, "api_url": base_url } # Execute the GET request response = requests.get(request_url, params=params) # Handle the response if response.status_code == 200: # v1 returns XML by default; add "&output=json" to params for JSON contact_lists = response.text print(contact_lists) else: print(f"Request failed: {response.status_code}") print(response.text)
Recommended: Use the v3 API (Current Supported Version)
The v3 API is more modern, RESTful, and natively supports JSON. For retrieving contact lists, here’s how to structure the request properly:
- Base URL:
https://url.api-us1.com/api/3 - Endpoint:
/contacts/lists - Authentication: Pass your API key in the
Api-Tokenrequest header
v3 Request Code
import requests api_key = "YOUR_ACTIVECAMPAIGN_API_KEY" base_url = "https://url.api-us1.com/api/3" # Set up authentication headers headers = { "Api-Token": api_key, "Content-Type": "application/json" } # Execute the GET request to the contact lists endpoint response = requests.get(f"{base_url}/contacts/lists", headers=headers) # Handle the response if response.status_code == 200: # v3 returns JSON by default contact_lists = response.json() print(contact_lists) else: print(f"Request failed: {response.status_code}") print(response.json())
Key Tips
- Always replace
YOUR_ACTIVECAMPAIGN_API_KEYwith the actual key from your account settings. - For the v1 API, ensure the
api_urlparameter exactly matches your base URL (no trailing slashes). - If you’re building a new integration, stick with v3—it’s the officially supported version with better documentation and functionality.
内容的提问来源于stack exchange,提问作者RustyShackleford

