{"id":14064704,"url":"https://github.com/Fewsats/L402-python","last_synced_at":"2025-07-29T18:33:52.139Z","repository":{"id":240287776,"uuid":"749985991","full_name":"Fewsats/L402-python","owner":"Fewsats","description":"L402 client and server library for python projects","archived":false,"fork":false,"pushed_at":"2024-10-17T20:04:53.000Z","size":1673,"stargazers_count":12,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-18T18:08:35.932Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Fewsats.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-29T19:18:06.000Z","updated_at":"2024-10-17T20:04:56.000Z","dependencies_parsed_at":"2024-06-09T14:01:19.264Z","dependency_job_id":"ce789fd9-fcff-4a42-a196-076335605d68","html_url":"https://github.com/Fewsats/L402-python","commit_stats":null,"previous_names":["fewsats/l402-python"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fewsats%2FL402-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fewsats%2FL402-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fewsats%2FL402-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Fewsats%2FL402-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Fewsats","download_url":"https://codeload.github.com/Fewsats/L402-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228040824,"owners_count":17860211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-13T07:04:01.770Z","updated_at":"2025-07-29T18:33:52.128Z","avatar_url":"https://github.com/Fewsats.png","language":"Python","funding_links":[],"categories":["Crypto Payment Rails","Libraries","Python","Crypto payment rails"],"sub_categories":["Fewsats"],"readme":"# l402-python\n\n\n\u003c!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! --\u003e\n\n## Usage\n\n### Installation\n\nInstall latest from [pypi](https://pypi.org/project/l402/)\n\n``` sh\n$ pip install l402\n```\n\n### Documentation\n\nDocumentation can be found hosted on this GitHub\n[repository](https://github.com/Fewsats/l402-python)’s\n[pages](https://Fewsats.github.io/l402-python/). Additionally you can\nfind package manager specific guidelines on\n[conda](https://anaconda.org/Fewsats/l402-python) and\n[pypi](https://pypi.org/project/l402-python/) respectively.\n\n## How to use\n\n### Paying with the L402\n\nThe L402 supports multiple payment providers. In this example we’ll use\na coinbase wallet as an onchain provider. This wallet will be used to\npay for L402 offers.\n\n``` python\nfrom l402.payment_clients import Client, CoinbaseProvider\nfrom cdp import Wallet\n```\n\n``` python\n# CDP can be directly used as an onchain provider.\nw = Wallet.create()\n\nc = Client(onchain_provider=CoinbaseProvider(wallet=w))\n```\n\nNext we retrieve an offer from a demo server. This will return a 402\nstatus code and a JSON response with the available offers.\n\n``` python\nr1 = httpx.get(\"http://localhost:9000/offers\")\nr1.status_code, r1.json()\n```\n\n    (402,\n     {'offers': [{'amount': 1,\n        'currency': 'USD',\n        'description': 'Purchase 1 credit for API access',\n        'offer_id': '58e1ee0a-e283-4f80-8a41-5c2f0ea138ca',\n        'payment_methods': ['onchain'],\n        'title': '1 Credit Package',\n        'type': 'one-time'}],\n      'payment_context_token': '42f46106-8989-4ed1-a770-adf9ad64c30b',\n      'payment_request_url': 'http://localhost:9000/payment_request',\n      'version': '0.2.2'})\n\nTo pay, we just need to provide the L402 response to the client and it\nwill automatically use configured payment provider that matches the\npayment method.\n\n``` python\nif r1.status_code == HTTPStatus.PAYMENT_REQUIRED:\n    ps = c.pay(r1.json())\n```\n\n### Getting paid with the L402\n\nThe L402 protocol implements a two-step payment flow:\n\n1.  The server returns a `402 Payment Required` status code and\n    information about the available offers.\n2.  The client uses the L402 response to fetch the information for a\n    specific payment method.\n\nIn this example, we implement this using a server with two endpoints:\n\n1.  `/offers` - Returns available payment options with 402 status\n2.  `/payment_request` - Generates payment details for chosen payment\n    method\n\n``` python\nfrom l402.payment_providers import PaymentServer, Offer\n\napp = FastAPI()\n\n\nps = PaymentServer(\n    payment_request_url=\"http://localhost:9000/payment_request\",\n    onchain_provider=w,\n)\n\n@app.get(\"/offers\")\ndef offers():\n    offers_list = [Offer(\n        amount=1,\n        currency='USD',\n        description='Purchase 1 credit for API access',\n        offer_id=str(uuid4()),\n        payment_methods=['onchain'],\n        title='1 Credit Package',\n        type='one-time'\n    )]\n    \n    offers_response = ps.create_offers(offers_list)\n    # Return the status code 402 Payment Required with the offers\n    return JSONResponse(content=offers_response.model_dump(), status_code=402)\n\n@app.post(\"/payment_request\")\nasync def create_payment_request(request: PaymentRequest):\n    payment_request = ps.create_payment_request(**request.model_dump())\n    return JSONResponse(content=payment_request)\n```\n\n``` python\nr = httpx.get(\"http://localhost:9000/offers\")\noffers_data = r.json()\nr.status_code, offers_data\n```\n\n    (402,\n     {'offers': [{'amount': 1,\n        'currency': 'USD',\n        'description': 'Purchase 1 credit for API access',\n        'offer_id': '1808e12c-1824-49d8-a348-278959f6008d',\n        'payment_methods': ['onchain'],\n        'title': '1 Credit Package',\n        'type': 'one-time'}],\n      'payment_context_token': '82dbf89a-b18f-4737-b350-c58731b1cc18',\n      'payment_request_url': 'http://localhost:9000/payment_request',\n      'version': '0.2.2'})\n\nThe client can use this offer to request the information for a payment\nmethod. We have to choose the payment method and, in the case of\ncoinbase, the chain and asset.\n\n``` python\noffers_data = dict2obj(offers_data)\ndata = {\n    \"offer_id\": offers_data.offers[0].offer_id, \n    \"payment_method\": 'onchain',\n    \"chain\": 'base-sepolia', # choose the chain where you want to pay\n    \"asset\": 'usdc', # choose the asset you want to pay\n    \"payment_context_token\": offers_data.payment_context_token \n}\nr = httpx.post(offers_data.payment_request_url, json=data)\nr, r.json()\n```\n\n    (\u003cResponse [200 OK]\u003e,\n     {'expires_at': '2025-01-30T21:03:46.502392+00:00',\n      'offer_id': '1808e12c-1824-49d8-a348-278959f6008d',\n      'payment_request': {'address': '0x6EC717D99534aF3917E25De1024f9e5BcDc1B262',\n       'chain': 'base-sepolia',\n       'asset': 'usdc'},\n      'version': '0.2.2'})\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFewsats%2FL402-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFewsats%2FL402-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFewsats%2FL402-python/lists"}