|
| 1 | +# Iteration Layer Python SDK |
| 2 | + |
| 3 | +Official Python SDK for the [Iteration Layer API](https://iterationlayer.com). |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +pip install iterationlayer |
| 9 | +``` |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +```python |
| 14 | +from iterationlayer import IterationLayer |
| 15 | + |
| 16 | +client = IterationLayer(api_key="il_your_api_key") |
| 17 | +``` |
| 18 | + |
| 19 | +### Document Extraction |
| 20 | + |
| 21 | +Extract structured data from documents using AI. |
| 22 | + |
| 23 | +```python |
| 24 | +result = client.extract( |
| 25 | + files=[{"type": "url", "name": "invoice.pdf", "url": "https://example.com/invoice.pdf"}], |
| 26 | + schema={ |
| 27 | + "fields": [ |
| 28 | + {"type": "TEXT", "name": "company_name", "description": "The company name"}, |
| 29 | + {"type": "CURRENCY_AMOUNT", "name": "total", "description": "The invoice total"}, |
| 30 | + ] |
| 31 | + }, |
| 32 | +) |
| 33 | + |
| 34 | +print(result["company_name"]["value"]) # "Acme Corp" |
| 35 | +print(result["company_name"]["confidence"]) # 0.95 |
| 36 | +``` |
| 37 | + |
| 38 | +### Image Transformation |
| 39 | + |
| 40 | +Resize, crop, convert, and apply effects to images. |
| 41 | + |
| 42 | +```python |
| 43 | +result = client.transform( |
| 44 | + file={"type": "url", "name": "photo.jpg", "url": "https://example.com/photo.jpg"}, |
| 45 | + operations=[ |
| 46 | + {"type": "resize", "width_in_px": 800, "height_in_px": 600, "fit": "cover"}, |
| 47 | + {"type": "convert", "format": "webp", "quality": 85}, |
| 48 | + ], |
| 49 | +) |
| 50 | + |
| 51 | +import base64 |
| 52 | +image_bytes = base64.b64decode(result["buffer"]) |
| 53 | +``` |
| 54 | + |
| 55 | +### Image Generation |
| 56 | + |
| 57 | +Compose images from layer definitions. |
| 58 | + |
| 59 | +```python |
| 60 | +result = client.generate_image( |
| 61 | + dimensions={"width_in_px": 1200, "height_in_px": 630}, |
| 62 | + layers=[ |
| 63 | + {"type": "solid-color-background", "index": 0, "hex_color": "#1a1a2e"}, |
| 64 | + { |
| 65 | + "type": "text", |
| 66 | + "index": 1, |
| 67 | + "text": "Hello World", |
| 68 | + "font_name": "Inter", |
| 69 | + "font_size_in_px": 48, |
| 70 | + "text_color": "#ffffff", |
| 71 | + "position": {"x_in_px": 50, "y_in_px": 50}, |
| 72 | + "dimensions": {"width_in_px": 1100, "height_in_px": 530}, |
| 73 | + }, |
| 74 | + ], |
| 75 | + output_format="png", |
| 76 | +) |
| 77 | + |
| 78 | +import base64 |
| 79 | +image_bytes = base64.b64decode(result["buffer"]) |
| 80 | +``` |
| 81 | + |
| 82 | +### Document Generation |
| 83 | + |
| 84 | +Generate PDF, DOCX, EPUB, or PPTX from structured data. |
| 85 | + |
| 86 | +```python |
| 87 | +result = client.generate_document( |
| 88 | + format="pdf", |
| 89 | + document={ |
| 90 | + "metadata": {"title": "Invoice #123"}, |
| 91 | + "page": { |
| 92 | + "size": {"preset": "A4"}, |
| 93 | + "margins": {"top_in_pt": 36, "bottom_in_pt": 36, "left_in_pt": 36, "right_in_pt": 36}, |
| 94 | + }, |
| 95 | + "styles": { |
| 96 | + "text": {"font_family": "Helvetica", "font_size_in_pt": 12, "line_height": 1.5, "color": "#000000"}, |
| 97 | + "headline": {"font_family": "Helvetica", "font_size_in_pt": 24, "color": "#000000", "spacing_before_in_pt": 12, "spacing_after_in_pt": 6}, |
| 98 | + "link": {"color": "#0066cc"}, |
| 99 | + "list": {"indent_in_pt": 18, "spacing_between_items_in_pt": 4}, |
| 100 | + "table": { |
| 101 | + "header": {"background_color": "#f0f0f0", "font_family": "Helvetica", "font_size_in_pt": 12, "color": "#000000", "padding_in_pt": 6}, |
| 102 | + "body": {"font_family": "Helvetica", "font_size_in_pt": 12, "color": "#000000", "padding_in_pt": 6}, |
| 103 | + }, |
| 104 | + "grid": {"gap_in_pt": 12}, |
| 105 | + "separator": {"color": "#cccccc", "thickness_in_pt": 1, "margin_top_in_pt": 12, "margin_bottom_in_pt": 12}, |
| 106 | + "image": {"alignment": "center", "margin_top_in_pt": 8, "margin_bottom_in_pt": 8}, |
| 107 | + }, |
| 108 | + "content": [ |
| 109 | + {"type": "headline", "level": "h1", "text": "Invoice #123"}, |
| 110 | + {"type": "paragraph", "markdown": "Thank you for your business."}, |
| 111 | + ], |
| 112 | + }, |
| 113 | +) |
| 114 | + |
| 115 | +import base64 |
| 116 | +pdf_bytes = base64.b64decode(result["buffer"]) |
| 117 | +``` |
| 118 | + |
| 119 | +### Webhooks (Async) |
| 120 | + |
| 121 | +Use the `*_async` methods to receive results via webhook instead of waiting for the response. |
| 122 | + |
| 123 | +```python |
| 124 | +result = client.extract_async( |
| 125 | + files=[{"type": "url", "name": "invoice.pdf", "url": "https://example.com/invoice.pdf"}], |
| 126 | + schema={ |
| 127 | + "fields": [ |
| 128 | + {"type": "CURRENCY_AMOUNT", "name": "total", "description": "The invoice total"}, |
| 129 | + ] |
| 130 | + }, |
| 131 | + webhook_url="https://your-app.com/webhooks/extraction", |
| 132 | +) |
| 133 | +``` |
| 134 | + |
| 135 | +### Context Manager |
| 136 | + |
| 137 | +The client can be used as a context manager to ensure the underlying HTTP connection is closed. |
| 138 | + |
| 139 | +```python |
| 140 | +with IterationLayer(api_key="il_your_api_key") as client: |
| 141 | + result = client.extract(...) |
| 142 | +``` |
| 143 | + |
| 144 | +### Error Handling |
| 145 | + |
| 146 | +```python |
| 147 | +from iterationlayer import IterationLayerError |
| 148 | + |
| 149 | +try: |
| 150 | + result = client.extract(...) |
| 151 | +except IterationLayerError as e: |
| 152 | + print(e.status_code) # 422 |
| 153 | + print(e.error_message) # "Validation error: ..." |
| 154 | +``` |
| 155 | + |
| 156 | +## Documentation |
| 157 | + |
| 158 | +Full documentation is available at [https://iterationlayer.com/docs](https://iterationlayer.com/docs). |
| 159 | + |
| 160 | +## Issues & Feedback |
| 161 | + |
| 162 | +Please report bugs and request features in the [issues repository](https://github.com/iterationlayer/issues). |
0 commit comments