# fewsats


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## What’s this all about?

[L402](https://l402.org) enables internet-native payments using the HTTP
status code `402 Payment Required`. It’s built on the [Lightning
Network](https://lightning.network/), a layer on top of Bitcoin, making
it extremely fast. But there’s a catch: not many people have Bitcoin to
spend.

That’s where [Hub](https://paywithhub.com) comes in. It lets you use
dollars to buy L402 links, keeping the advantages of L402 without
needing cryptocurrency.

This library helps your AI agents leverage Hub to access paywalled
content programmatically. No crypto knowledge required.

## Install

``` sh
pip install fewsats
```

## How to use

Before using the library, you need to sign up at https://paywithhub.com.
Verify your email, and then generate an API key.

First, import the Fewsats class:

``` python
from fewsats import Fewsats
```

Initialize the Fewsats client:

``` python
client = Fewsats(api_key="nJ2ro5V9x69JDEhEGyjnzfg6kTkk4LQjukIbfDmuOec",
                 base_url="http://localhost:8000")
# Leave base_url blank to use the default production URL
# You can also set the HUB_API_TOKEN environment variable instead of passing the api_key directly
```

## Get Payment Methods

You can retrieve the user’s payment methods using the
`get_payment_methods` method:

``` python
payment_methods = client.get_payment_methods()
payment_methods
```

    [{'id': 1,
      'last4': '4242',
      'brand': 'visa',
      'exp_month': 12,
      'exp_year': 2034,
      'is_default': True}]

**Note**: The default payment method is always used when making
purchases.

## Preview a purchase

You can preview a purchase using the `preview_purchase` method:

``` python
l402_url = "https://api.fewsats.com/v0/storage/download/2e54ac29-5945-4b5f-93db-998a535a5a49"
preview = client.preview_purchase(l402_url)
preview
```

    {'invoice': {'description': 'USK House',
      'amount_usd': 1,
      'amount_btc': 17,
      'macaroon': 'AgELZmV3c2F0cy5jb20CQgAAN46L7KQ/jcRtps3kttV7Vn8MDtylepV2oPxRMFq6SKMXWUCBx3QoBJ7yvDUbRrTHVVtFLVAxPWOR66CfzNiCBwACLGZpbGVfaWQ9MmU1NGFjMjktNTk0NS00YjVmLTkzZGItOTk4YTUzNWE1YTQ5AAIfZXhwaXJlc19hdD0yMDI1LTA4LTI0VDEzOjMzOjQ0WgAABiAZNDXYCl3z+KyWRma4w+nQ+cRR3yjrnIr2gi0P2mEfhw==',
      'invoice': 'lnbc170n1pndq79cpp5x78ghm9y87xugmdxehjtd4tm2elscrku54af2a4ql3gnqk46fz3sdq024f5kgzgda6hxegcqzzsxqyz5vqsp5tdndqcefruge6y03x3cuad9xggwfucs5ae7e88l09wxu3kt20gvs9qxpqysgquvm3u7na6sn77v0rwt0uewqp768mw380wghqumv6d3spcd99656jjnt9rmxqkursfl9c3x70j6f7wa2qzrjfm3th0jwnlqvkdnnqwcgqavs2vv'},
     'transaction': {'current_balance': 1999,
      'balance_to_apply': 1,
      'amount_to_charge': 0,
      'final_balance': 1998},
     'already_purchased': True,
     'purchase': {'id': 3,
      'user_id': 1,
      'l402_url': 'https://api.fewsats.com/v0/storage/download/2e54ac29-5945-4b5f-93db-998a535a5a49',
      'currency': 'usd',
      'amount': 1,
      'macaroon': 'AgELZmV3c2F0cy5jb20CQgAADkZv2VQjgOG+iwoFB5CXg5yInkncy5xEGQZKrkphETmU9+MjeNM4bsj1j5l5KKvildd2pHdtrW+ChXQOQ/fZzgACLGZpbGVfaWQ9MmU1NGFjMjktNTk0NS00YjVmLTkzZGItOTk4YTUzNWE1YTQ5AAIfZXhwaXJlc19hdD0yMDI1LTA4LTIzVDE0OjQ3OjI1WgAABiBIoB5sUXEXijxuyL2cjn0WIgLpOCtt8UcGaGn9N/8n0Q==',
      'invoice': 'lnbc170n1pnv7wrapp5perxlk25ywqwr05tpgzs0yyhswwg38jfmn9ec3qeqe92ujnpzyusdq024f5kgzgda6hxegcqzzsxqyz5vqsp54ceypjapjxntxrtnhkyfrds7m5u9stn8x6gkmf5dx6z56ksy6l3q9qxpqysgqnkxdtwqxhkvrav3d3wv83yxn45yrdywxu2mnyh4p7ecncuqsv4gn0xrfl6w57hd9lnc5eghdf536dqy5gf2uuly04nrtdjcwvezkt6cqtttnzj',
      'description': 'USK House',
      'preimage': 'b184b87e7a4f802bf41cd26c82d9d883b9cf7bb86f8dcfc4f394634ff711f628',
      'valid': True,
      'created_at': '2024-08-28T14:47:26.904Z'}}

## Make a purchase

To make a purchase, use the `purchase` method:

``` python
purchase = client.purchase(l402_url)
purchase
```

    {'id': 3,
     'created_at': '2024-08-28T14:47:26.904Z',
     'l402_url': 'https://api.fewsats.com/v0/storage/download/2e54ac29-5945-4b5f-93db-998a535a5a49',
     'macaroon': 'AgELZmV3c2F0cy5jb20CQgAADkZv2VQjgOG+iwoFB5CXg5yInkncy5xEGQZKrkphETmU9+MjeNM4bsj1j5l5KKvildd2pHdtrW+ChXQOQ/fZzgACLGZpbGVfaWQ9MmU1NGFjMjktNTk0NS00YjVmLTkzZGItOTk4YTUzNWE1YTQ5AAIfZXhwaXJlc19hdD0yMDI1LTA4LTIzVDE0OjQ3OjI1WgAABiBIoB5sUXEXijxuyL2cjn0WIgLpOCtt8UcGaGn9N/8n0Q==',
     'invoice': 'lnbc170n1pnv7wrapp5perxlk25ywqwr05tpgzs0yyhswwg38jfmn9ec3qeqe92ujnpzyusdq024f5kgzgda6hxegcqzzsxqyz5vqsp54ceypjapjxntxrtnhkyfrds7m5u9stn8x6gkmf5dx6z56ksy6l3q9qxpqysgqnkxdtwqxhkvrav3d3wv83yxn45yrdywxu2mnyh4p7ecncuqsv4gn0xrfl6w57hd9lnc5eghdf536dqy5gf2uuly04nrtdjcwvezkt6cqtttnzj',
     'preimage': 'b184b87e7a4f802bf41cd26c82d9d883b9cf7bb86f8dcfc4f394634ff711f628',
     'amount': 1,
     'currency': 'usd',
     'description': 'USK House'}
