Generate code to integrate with external APIs
Workflow Description: Generate code to integrate with external APIs
Step: collab → generate_code
This deliverable provides comprehensive, production-ready code examples for integrating with external APIs. As part of the "API Integration Builder" workflow, this step focuses on generating a foundational codebase that you can adapt and extend for your specific API integration needs.
Given the broad nature of "API Integration Builder" without a specific target API, we have generated flexible and robust code examples for a common RESTful API integration scenario. We will use JSONPlaceholder (a free fake REST API) as our example to demonstrate typical GET and POST operations, robust error handling, and best practices in both Python and JavaScript (Node.js).
This output is designed to be directly actionable, providing you with a solid starting point for your API integration projects.
Before diving into the code, it's crucial to understand the principles guiding effective API integration:
POST, PUT, and DELETE operations.We will demonstrate integration with JSONPlaceholder, a simple REST API that provides dummy data. This allows us to show common operations without needing complex authentication or specific business logic.
API Base URL: https://jsonplaceholder.typicode.com
Example Endpoints:
GET /posts: Retrieve a list of posts.GET /posts/{id}: Retrieve a single post by ID.POST /posts: Create a new post.We provide code examples in two popular languages for API integration: Python and JavaScript (Node.js). Each example includes a client class/module, configuration, and demonstration of GET and POST requests with error handling.
This Python client uses the requests library, which is the de-facto standard for HTTP requests in Python.
Dependencies:
requests: pip install requestsapi_client.py
import requests
import json
import os
import logging
# Configure logging for better visibility
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class APIIntegrationError(Exception):
"""Custom exception for API integration specific errors."""
def __init__(self, message, status_code=None, response_data=None):
super().__init__(message)
self.status_code = status_code
self.response_data = response_data
class JSONPlaceholderClient:
"""
A robust client for interacting with the JSONPlaceholder API.
Handles configuration, HTTP requests (GET, POST), and error handling.
"""
def __init__(self, base_url=None, default_headers=None, timeout=30):
"""
Initializes the API client.
Args:
base_url (str, optional): The base URL for the API.
Defaults to JSONPlaceholder if not provided.
Can also be set via JPH_BASE_URL environment variable.
default_headers (dict, optional): Default headers to send with each request.
timeout (int): Default timeout for requests in seconds.
"""
# Prioritize explicit base_url, then environment variable, then default
self.base_url = base_url or os.getenv('JPH_BASE_URL', 'https://jsonplaceholder.typicode.com')
self.default_headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
**(default_headers or {}) # Merge any custom default headers
}
self.timeout = timeout
logging.info(f"Initialized JSONPlaceholderClient with base URL: {self.base_url}")
def _request(self, method, endpoint, params=None, data=None, headers=None):
"""
Internal method to handle all HTTP requests.
Args:
method (str): HTTP method (e.g., 'GET', 'POST').
endpoint (str): The API endpoint (e.g., '/posts').
params (dict, optional): Dictionary of URL query parameters.
data (dict or str, optional): Request body data. Will be JSON-encoded if dict.
headers (dict, optional): Additional headers for this specific request.
Returns:
dict: Parsed JSON response from the API.
Raises:
APIIntegrationError: If the request fails or returns a non-2xx status code.
"""
url = f"{self.base_url}{endpoint}"
request_headers = {**self.default_headers, **(headers or {})} # Merge default and specific headers
try:
logging.debug(f"Making {method} request to: {url}")
logging.debug(f"Params: {params}, Data: {data}, Headers: {request_headers}")
response = requests.request(
method,
url,
params=params,
json=data if isinstance(data, dict) else None, # Use json parameter for dicts
data=data if isinstance(data, str) else None, # Use data parameter for strings
headers=request_headers,
timeout=self.timeout
)
response.raise_for_status() # Raises HTTPError for bad responses (4xx or 5xx)
logging.info(f"Successful {method} request to {endpoint}. Status: {response.status_code}")
return response.json()
except requests.exceptions.Timeout:
logging.error(f"Request timed out after {self.timeout} seconds for {url}")
raise APIIntegrationError(f"Request timed out for {url}", status_code=408)
except requests.exceptions.ConnectionError as e:
logging.error(f"Network connection error for {url}: {e}")
raise APIIntegrationError(f"Network connection error: {e}", status_code=503)
except requests.exceptions.HTTPError as e:
status_code = e.response.status_code
try:
error_data = e.response.json()
except json.JSONDecodeError:
error_data = e.response.text # Fallback to raw text if not JSON
logging.error(f"HTTP error {status_code} for {url}: {error_data}")
raise APIIntegrationError(f"API returned error: {error_data}",
status_code=status_code,
response_data=error_data)
except json.JSONDecodeError:
logging.error(f"Failed to decode JSON response from {url}. Response text: {response.text}")
raise APIIntegrationError(f"Invalid JSON response from {url}", status_code=500, response_data=response.text)
except Exception as e:
logging.error(f"An unexpected error occurred during request to {url}: {e}")
raise APIIntegrationError(f"An unexpected error occurred: {e}", status_code=500)
def get(self, endpoint, params=None, headers=None):
"""Performs a GET request."""
return self._request('GET', endpoint, params=params, headers=headers)
def post(self, endpoint, data, headers=None):
"""Performs a POST request."""
return self._request('POST', endpoint, data=data, headers=headers)
def put(self, endpoint, data, headers=None):
"""Performs a PUT request."""
return self._request('PUT', endpoint, data=data, headers=headers)
def delete(self, endpoint, headers=None):
"""Performs a DELETE request."""
return self._request('DELETE', endpoint, headers=headers)
# --- Example Usage ---
if __name__ == "__main__":
# Optional: Set base URL via environment variable
# os.environ['JPH_BASE_URL'] = 'https://jsonplaceholder.typicode.com'
client = JSONPlaceholderClient()
print("\n--- Fetching all posts ---")
try:
posts = client.get('/posts')
print(f"Fetched {len(posts)} posts. First post title: {posts[0]['title']}")
except APIIntegrationError as e:
print(f"Error fetching posts: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("\n--- Fetching a single post (ID 1) ---")
try:
post_id = 1
single_post = client.get(f'/posts/{post_id}')
print(f"Fetched post {post_id}: {single_post['title']}")
except APIIntegrationError as e:
print(f"Error fetching post {post_id}: {e}")
print("\n--- Creating a new post ---")
new_post_data = {
'title': 'My New API Post',
'body': 'This is the body of my new post created via the API client.',
'userId': 1
}
try:
created_post = client.post('/posts', data=new_post_data)
print(f"Successfully created post with ID: {created_post['id']}")
print(f"Created post title: {created_post['title']}")
except APIIntegrationError as e:
print(f"Error creating post: {e}")
print("\n--- Demonstrating error handling (non-existent endpoint) ---")
try:
client.get('/nonexistent-endpoint')
except APIIntegrationError as e:
print(f"Caught expected error: {e.status_code} - {e.response_data}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
print("\n--- Demonstrating error handling (invalid post ID) ---")
try:
client.get('/posts/999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
Project Status: Initiated
We are pleased to confirm the successful creation of your new API Integration Project within the PantheraHive platform. This marks the official commencement of our engagement to build robust and efficient integrations for your specified systems.
Project Name: API Integration Initiative
Project ID: [Auto-Generated Project ID - e.g., PH-API-20231027-001]
Creation Date: October 27, 2023
Current Status: Project Created - Awaiting Detailed Requirements
This project is dedicated to leveraging the "API Integration Builder" to establish seamless, secure, and performant connections between your internal systems and external APIs. Our primary objective is to automate data exchange, streamline workflows, enhance system interoperability, and unlock new capabilities for your business.
Based on the initial input provided to the API Integration Builder, the high-level objectives for this project include:
[Placeholder: System A] and [Placeholder: External API/System B].[Placeholder: e.g., one-way synchronization, bi-directional data exchange, event-driven triggers] for [Placeholder: e.g., customer data, order status, inventory updates, financial transactions].Note: A detailed scope will be collaboratively defined in the upcoming "Requirements Gathering" phase.
This API integration project will proceed through the following structured phases to ensure a high-quality and successful deployment:
This crucial phase focuses on a deep dive into your specific needs and the technical intricacies of the APIs involved.
* Stakeholder Workshops: Conduct sessions with your business and technical teams to capture precise integration requirements, use cases, and business logic.
* API Documentation Review: Thorough analysis of all relevant API documentation (source and target systems) to understand endpoints, data models, authentication mechanisms, rate limits, and error handling.
* Data Mapping: Define exact field-to-field mappings and any required data transformations.
* Integration Flow Design: Architect the complete integration workflow, including triggers, sequence of operations, error handling, retry mechanisms, and logging strategies.
* Security Assessment: Review and design secure authentication, authorization, and data encryption practices.
* Integration Design Document (IDD): A comprehensive document outlining the technical architecture, data flows, API specifications, error handling, security considerations, and deployment strategy.
* Detailed Data Mapping Specification.
Leveraging the insights from the design phase, our team will proceed with the development of the integration solution.
* Code Scaffolding: Utilize the API Integration Builder to generate foundational code based on the IDD.
* Custom Logic Implementation: Develop and integrate any custom business logic, data transformation routines, and complex orchestration required.
* API Client Development: Create robust and efficient API clients/connectors for all involved APIs.
* Secure Credential Management: Implement best practices for handling API keys and sensitive credentials.
* Production-Ready Integration Codebase: Fully commented and structured code.
* Deployment Scripts & Instructions.
Rigorous testing is performed to ensure the integration is functional, reliable, and performs as expected under various conditions.
* Unit Testing: Verify individual components and functions.
* Integration Testing: Test the interaction between different integrated systems.
* End-to-End Testing: Simulate real-world scenarios across the entire workflow.
* Performance & Load Testing: Assess the integration's behavior under expected and peak loads.
* User Acceptance Testing (UAT): Collaborate with your team to validate the solution against business requirements in a staging environment.
* Comprehensive Test Reports.
* UAT Sign-off.
The final phase involves deploying the integration to your production environment and establishing ongoing operational support.
* Production Deployment: Execute the deployment plan in your live environment.
* Post-Deployment Verification: Confirm successful deployment and initial operational status.
* Monitoring & Alerting Setup: Configure dashboards and alerts to proactively track integration health, performance, and error rates.
* Documentation Handover: Provide complete operational documentation.
* Live API Integration Solution.
* Monitoring Dashboards & Alerts.
* Operational Runbook.
Your active participation and timely provision of information are critical to the success of this project. During the upcoming phases, we will require:
* Full API documentation for all systems to be integrated.
* Access to developer portals and sandbox/staging environments.
* Secure provision of API keys, tokens, or credentials (we will provide secure channels for this).
* Availability of subject matter experts (SMEs) from both business and IT departments.
* Clear articulation of business rules, data transformation logic, and specific use cases.
* Provision of dedicated development, staging, and UAT environments.
* Sample data for comprehensive testing.
* Timely review and approval of design documents, test plans, and UAT sign-offs.
A detailed project timeline, resource allocation, and cost estimates will be provided upon the completion of the "Detailed Requirements Gathering & Technical Design" phase. This ensures accuracy based on a thorough understanding of the integration scope.
Your dedicated Project Manager will be your primary point of contact for all project-related communications.
[Project Manager Name] * Email: [Project Manager Email]
* Phone: [Project Manager Phone]
Our team is excited to embark on this integration journey with you. We look forward to a successful collaboration.