Core API Python Client

2.3.3 · deprecated · verified Sat Apr 11

The `coreapi` library is a Python client for interacting with web APIs that expose Core API schemas or hypermedia formats. It supports formats like CoreJSON, OpenAPI (for schema description), JSON Hyper-Schema, and HAL. The latest stable version, 2.3.3, was released in October 2017. This library is largely considered superseded, particularly within the Django REST Framework ecosystem where its schema generation was deprecated in favor of OpenAPI.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a `coreapi.Client`, fetch an API document (schema), and perform an action (like listing resources) based on the document's structure. It uses `http://notes.coreapi.org/` as an example API endpoint.

import coreapi
import os

# The Core API library interacts with APIs based on their exposed schema/hypermedia.
# We'll use a public example service from coreapi.org for demonstration.
# Replace 'http://notes.coreapi.org/' with your actual Core API endpoint.
API_URL = os.environ.get('COREAPI_EXAMPLE_URL', 'http://notes.coreapi.org/')

try:
    # 1. Create a client instance
    client = coreapi.Client()

    # 2. Retrieve the API document (schema/hypermedia description)
    document = client.get(API_URL)
    print(f"Successfully retrieved API document for: {document.title}")

    # 3. Interact with the API using actions defined in the document
    # This example assumes the 'notes' service at API_URL has a 'list' action.
    # Adjust 'keys' and 'params' based on the specific API you are interacting with.
    if 'notes' in document and 'list' in document['notes']:
        print("\nAttempting to list notes...")
        notes_list = client.action(document, ['notes', 'list'])
        print(f"Retrieved {len(notes_list)} notes.")
        for note in notes_list:
            print(f"- {note.get('description', 'No description')}")
    else:
        print(f"'notes' or 'list' action not found in document from {API_URL}. Cannot demonstrate interaction.")

except coreapi.exceptions.NetworkError as e:
    print(f"Error: Network issue connecting to {API_URL}. Details: {e}")
    print("Please ensure the API endpoint is correct and accessible.")
except coreapi.exceptions.ErrorMessage as e:
    print(f"Error: API returned an error message. Status: {e.status_code}, Detail: {e.error}")
except coreapi.exceptions.LinkLookupError as e:
    print(f"Error: Invalid action path. Details: {e}")
    print("Check the keys passed to client.action() match the API document structure.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →