Skip to main content

PyLapi

Python Lightweight API (PyLapi) is a Python API builder.

It takes only a few seconds to automatically generate a Python API from OpenAPI specifications or less than an hour for an experienced Python developer to create a custom Python API.

You can drive PyLapi in an automatic, semiautomatic, or manual way.

Automatic:

The auto-generated Python API contains fully functional resource classes with API methods embedded with OpenAPI specifications as user guide inline comments, so you don't need to read the lengthy OpenAPI Specification to study or use the API.

For more details, please see this tutorial on how to use the generated API.

Semiautomatic:

You can also manually "rewrite" some of the generated classes and methods in a separate code file to, for example, specify custom parameters, pre-process the request, or post-process the response. The PyLapi generator will merge your code into the generated Python API automatically.

PyLapi's code-rewrite feature allows you to specify different API URL endpoints for individual resources, effectively combining multiple API backend services into a single frontend API. All parameters and the request body are dynamically mapped based on the API method route, with the option to rewrite them if you so choose.

More details can be found in this tutorial.

Manual:

When OpenAPI Specifications are not available or you want to create a Python API your own way, you can inherit your root API class from PyLapi, which does all the heavy lifting for you, so your code would mostly be a pass.

If you want to manipulate the API requests and responses, only minimal coding is required. In the tutorial A ChatGPT Conversation with PyLapi, a fully functional Conversation resource is completed in 38 lines, allowing you to do cool things like this:

conversation.ask("Where is the Sydney Opera House?")
print(conversation.data.answers[0])
# Output:
# The Sydney Opera House is located in Sydney, Australia. Specifically, it is situated on Bennelong Point in the Sydney Harbour, close to the Sydney Harbour Bridge.

When OpenAPI Specifications are not available or you want to create a Python API your own way, you can inherit your API class from PyLapi, which does all the heavy lifting for you, so your code would mostly be a pass. For example,

More details can be found in this tutorial.

A PyLapi code example

Here is a sample of a PyLapi API:

from pylapi import PyLapi

class MyAPI(PyLapi):
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.api_url = "https://api.example.com"
        self.api_auth_type = "Bearer"

# resource_name="invoice", resource_base_path="invoices"
@MyAPI.resource_class("invoice", "invoices")
class InvoiceResource(MyAPI):

    @MyAPI.resource_method("{invoice_number}", http_method="GET")
    def getInvoice(self): pass
    # To call: MyAPI.resource("invoice").getInvoice(...)
    # Request: GET https://api.example.com/invoices/{invoice_number}

The MyAPI API root class inherits the decorators @MyAPI.resource_class and @MyAPI.resource_methods from PyLapi, which prepares the API parameters and the request body in the API request, sends the request to the API URL endpoint, processes the response, and handles the resource data for you.


The PyLapi framework makes it very easy and natural for you to use a PyLapi-supported Python API. For example,

from myapi import MyAPI

MyAPI.auth(AUTH_ACCESS_TOKEN)

invoice = MyAPI.resource("invoice")
invoice.data = invoice.getInvoice("12345678") # using positional arguments
print(invoice)

item_list = invoice.getItemList()
product = MyAPI.resource("product")
invoice_total = 0.0
for item in item_list:
    product.data.number = item["product_number"]
    product.data = product.getProduct() # using implicit arguments from product.data
    subtotal = product.data.price * item["qty"]
    invoice_total += subtotal
    print(f"Item: {product.data.name}")
    print(f"  Price: ${product.data.price:0.2f}")
    print(f"  Qty: {item['qty']}")
    print(f"  Subtotal: ${subtotal:0.2f}")
print(f"Invoice Total: ${invoice_total:0.2f}")

Install PyLapi

Please follow these instructions to install PyLapi and its generator, replacing all variables accordingly.

To install the PyLapi class:

pip install pylapi

To generate a PyLapi supported Python API:

pylapi-autogen --template > myapi_config.py
# Configure myapi_config.py
pylapi-autogen myapi_config.py
# Output
# MyAPI generated and saved in ./myapi.py

More Information

PyLapi tutorial and user guide are available at the PyLapi GitHub repository. PyLapi API is documented on Read the Docs.

Metadata

Release files for pylapi 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pylapi 1.0.2
File Size Uploaded Provenance
pylapi-1.0.2.tar.gz 24.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pylapi 1.0.2
File Interpreter ABI Platform
pylapi-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 47.3 kB

Release files / pylapi-1.0.2.tar.gz

Download URL pylapi-1.0.2.tar.gz
Size 24.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c792ffb20f5e7bafe604c8261255b002b6502d4592e38e4796291c242e5d62d2
BLAKE2b-256 checksum
How to use checksums
5d4fb1f618857d679761601c1376aeadc2a275fce3041a5e128ce42a9271c318
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.5

Release files / pylapi-1.0.2-py3-none-any.whl

Download URL pylapi-1.0.2-py3-none-any.whl
Size 23.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b672029d29284971ba7df1d5bb60cb66bc67ad102e5a98a9f01e8e3009815529
BLAKE2b-256 checksum
How to use checksums
6f74290a2d4202aa7452f9384997a650fd6255731799b02ee242d35b92cc58c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.5

Security for pylapi 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page