home / skills / venkateshvenki404224 / frappe-apps-manager / frappe-external-api-connector

frappe-external-api-connector skill

/frappe-apps-manager/skills/frappe-external-api-connector

This skill generates robust API client code to connect Frappe with external REST services, handling authentication, errors, and data transformation.

npx playbooks add skill venkateshvenki404224/frappe-apps-manager --skill frappe-external-api-connector

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
3.9 KB
---
name: frappe-external-api-connector
description: Generate code to integrate Frappe with external REST APIs. Use when connecting to third-party services, payment gateways, or external data sources.
---

# Frappe External API Connector

Generate robust API client code for integrating Frappe with external REST APIs, handling authentication, error recovery, and data transformation.

## When to Use This Skill

Claude should invoke this skill when:
- User wants to integrate external REST APIs
- User needs to call third-party services
- User mentions API integration, external system connection
- User wants to integrate payment gateways, shipping APIs, etc.
- User needs OAuth or API key authentication

## Capabilities

### 1. API Client Class

**REST API Client Template:**
```python
import requests
import frappe
from frappe import _

class ExternalAPIClient:
    def __init__(self):
        self.base_url = frappe.conf.get('external_api_url')
        self.api_key = frappe.conf.get('external_api_key')
        self.timeout = 30

    def get_headers(self):
        return {
            'Authorization': f'Bearer {self.api_key}',
            'Content-Type': 'application/json',
            'User-Agent': 'Frappe/1.0'
        }

    def get(self, endpoint, params=None):
        """GET request with error handling"""
        try:
            response = requests.get(
                f'{self.base_url}/{endpoint}',
                params=params,
                headers=self.get_headers(),
                timeout=self.timeout
            )
            response.raise_for_status()
            return response.json()
        except requests.exceptions.Timeout:
            frappe.throw(_('Request timeout'))
        except requests.exceptions.HTTPError as e:
            self._handle_http_error(e)
        except Exception as e:
            frappe.log_error(frappe.get_traceback(),
                'External API Error')
            frappe.throw(_('API request failed'))

    def post(self, endpoint, data):
        """POST request"""
        response = requests.post(
            f'{self.base_url}/{endpoint}',
            json=data,
            headers=self.get_headers(),
            timeout=self.timeout
        )
        response.raise_for_status()
        return response.json()

    def _handle_http_error(self, error):
        """Handle HTTP errors"""
        status_code = error.response.status_code
        if status_code == 401:
            frappe.throw(_('API authentication failed'))
        elif status_code == 404:
            frappe.throw(_('Resource not found'))
        elif status_code == 429:
            frappe.throw(_('Rate limit exceeded'))
        else:
            frappe.throw(_(f'API error: {status_code}'))
```

### 2. OAuth Integration

**OAuth 2.0 Flow:**
```python
def get_oauth_token():
    """Get OAuth access token with refresh"""
    # Check cache first
    token = frappe.cache().get_value('oauth_token:provider')
    if token:
        return token

    # Get from settings
    settings = frappe.get_single('OAuth Settings')

    response = requests.post(
        settings.token_url,
        data={
            'grant_type': 'client_credentials',
            'client_id': settings.client_id,
            'client_secret': settings.get_password('client_secret')
        }
    )

    if response.status_code == 200:
        token_data = response.json()
        access_token = token_data['access_token']

        # Cache token
        frappe.cache().set_value(
            'oauth_token:provider',
            access_token,
            expires_in_sec=token_data.get('expires_in', 3600) - 60
        )

        return access_token

    frappe.throw(_('OAuth authentication failed'))
```

## References

**Frappe Integration Patterns:**
- Integrations: https://github.com/frappe/frappe/tree/develop/frappe/integrations
- ERPNext Integrations: https://github.com/frappe/erpnext/tree/develop/erpnext/erpnext_integrations

Overview

This skill generates ready-to-use code to integrate Frappe applications with external REST APIs. It focuses on secure authentication, resilient error handling, and clear data transformation patterns. Use it to produce client classes, OAuth token management, and example request/response logic you can drop into Frappe apps.

How this skill works

The skill outputs a Python API client template that reads configuration from frappe.conf, builds headers, and provides GET/POST helpers with timeout and HTTP error handling. It can also generate an OAuth 2.0 token flow that caches tokens in frappe.cache and refreshes when needed. Generated code includes sensible logging, frappe.throw for user-facing errors, and hooks for custom data mapping and retry policies.

When to use it

  • You need to call third-party REST APIs from a Frappe app.
  • Integrating payment gateways, shipping providers, or external CRMs.
  • When API access requires API keys, bearer tokens, or OAuth 2.0 client credentials.
  • When you want standardized error handling, rate-limit awareness, and token caching.
  • When building maintainable connectors that follow Frappe patterns.

Best practices

  • Store secrets and endpoints in frappe.conf or DocType settings and use get_password for credentials.
  • Cache OAuth tokens in frappe.cache and subtract a safety margin from expires_in before reuse.
  • Wrap external calls with timeouts and handle specific requests.exceptions for clearer feedback.
  • Raise user-friendly errors with frappe.throw and log full tracebacks with frappe.log_error for diagnostics.
  • Include idempotency and retry logic for safe POST operations and respect 429 rate-limit responses.

Example use cases

  • Generate an ExternalAPIClient class to call a shipping provider and map responses into Delivery Note fields.
  • Create an OAuth token helper to authenticate with a payment gateway and cache the access token.
  • Build a connector that polls a third-party CRM, transforms records, and creates Frappe Contacts.
  • Implement a webhook receiver that validates signatures and uses the client class to acknowledge events.
  • Scaffold integrations for inventory systems that require paginated GET requests and error recovery.

FAQ

Can generated code store credentials securely?

Yes. The code reads credentials from frappe.conf or a settings DocType and recommends using get_password and frappe.get_site_config to avoid hardcoding secrets.

How does token caching work?

Tokens are cached in frappe.cache with an expires_in minus a safety margin. The helper checks cache first and refreshes when missing or expired to minimize token requests.