Introducing the Anvil API Python Library
Tags: api, python, githunt, webdev
Published: 2021-04-26 04:46:06

The Anvil API is now supported on Python via our official client library. The library is available now on [GitHub](https://github.com/anvilco/python-anvil):

{% github anvilco/python-anvil no-readme %}

and as a package through PyPI via `pip install python-anvil` or your preferred package manager (i.e. pipenv, poetry).

As a company, we are heavily invested in the Javascript ecosystem; however, we also realize that making our platform more developer-friendly and accessible to other developers means increasing our official API library footprint.  That’s why we’ve added Python to our client library as another step towards building a more developer-friendly Anvil. We hope this release gives developers an opportunity to enjoy all that Anvil has to offer.

Features of this initial release:

* Keeping in line with current Javascript library, it includes:
    * [Filling PDFs](#filling-pdfs)
    * [Generating PDFs](#generating-pdfs)
    * [Creating Etch Signing Packets](#creating-etch-signing-packets)
* A [command-line interface (CLI)](#command-line-interface)
    * Contains the above, giving you the ability to test out our features in the Python REPL.
    * The `anvil` command from this package also has a few additional useful commands that will help you as you build out your integration, such as getting a list of PDF templates (or casts) for use in the “fill-pdf” API endpoint, as well as getting field data from a specific PDF template. All of this can be done from the comfort of your text-based terminal.

### Filling PDFs

[Filling PDFs](https://www.useanvil.com/docs/api/fill-pdf#filling-a-pdf-template) lets you fill PDF templates created on Anvil with a simple API call.

First, create a template on your Anvil dashboard:

![Dashboard](https://www.useanvil.com/static/upload-ac23b30d51d5ecf54cc7f592d573b26a.png)

Once uploaded, Anvil will attempt to find fields for you. You can also create your own fields and adjust any field types if you need something specific such as date formats.

When you finish editing fields, click over to the API Info tab. It shows everything you need to fill the template with the API, including an example payload to quickly get started.

![Template Info](https://www.useanvil.com/static/pdf-template-api-info-c0fa393ffc6c8ac44a3c24a9f1624616.png)

With the data on the API Info page, you can now create your API call. The resulting `response` will be the PDF file:

```python
from python_anvil.api import Anvil

anvil = Anvil(api_key="MY API KEY")
data = {
    "title": "IRS W-4",
    "font_size": 10,
    "data": {
        "someFieldName": "Example data",
        "aNumberField": 1234
    }
}
response = anvil.fill_pdf("template_id_here", data)
```

### Generating PDFs

The Anvil API also allows you to [generate new PDFs](https://www.useanvil.com/docs/api/generate-pdf) using provided JSON data. Useful for agreements, invoices, disclosures, or any other text-heavy documents.

```python
from python_anvil.api import Anvil

anvil = Anvil(api_key="MY API KEY")
data = {
    "title": "Example Invoice",
    "font_size": 12,
    "data": [{
        "content": "Supports **markdown**",
    }, {
        "table": {
            "rows": [
                ["Description", "Quantity", "Price"],
                ["4x Large Widgets", "4", "$40.00"],
                ["10x Medium Sized Widgets in dark blue", "10", "$100.00"],
                ["6x Small Widgets in white", "6", "$60.00"],
            ]
        }
    }]
}

response = anvil.generate_pdf("template_id_here", data)
```


### Creating Etch Signing Packets

The [Anvil Etch E-sign API](https://www.useanvil.com/docs/api/e-signatures) allows you to collect e-signatures from within your app. Send a signature packet including multiple PDFs, images, and other uploads to one or more signers. Templatize your common PDFs. Then, fill them with your user's information before sending out the signature packet.

This is one of the more complex methods in the API, so please take a look at the [Python Anvil docs](https://python-anvil.readthedocs.io/en/latest/advanced/#create-etch-packet) for a closer look on the process.


### Command-line interface

The included CLI gives you quick access to the methods above, as well as some additional GraphQL queries that can help with getting certain data without having to open up your browser.

When running the `anvil` command by itself, you'll be shown a display of support commands. Each individual command also has additional help documentation on how to use the command.

```bash
# The CLI commands will use the environment variable "ANVIL_API_KEY" for all
# Anvil API requests.
$ ANVIL_API_KEY=MY_GENERATED_KEY anvil
Usage: anvil [OPTIONS] COMMAND [ARGS]...

Options:
  --debug / --no-debug
  --help                Show this message and exit.

Commands:
  cast                Fetch Cast data given a Cast eid.
  create-etch         Create an etch packet with a JSON file.
  current-user        Show details about your API user
  download-documents  Download etch documents
  fill-pdf            Fill PDF template with data
  generate-etch-url   Generate an etch url for a signer
  generate-pdf        Generate a PDF
  weld                Fetch weld info or list of welds
```

Lastly, if you’re developing something cool with PDFs and/or paperwork automation, we’d love to hear more from you! Let us know at (email here) if you have any feedback. Also, if there are any suggestions on new features or issues you may encounter for this library, let us know on the Issue section of the GitHub repo page, and we’ll get back to you.