-
Notifications
You must be signed in to change notification settings - Fork 976
feat: add Python SDK v2 server examples (FastAPI, Flask) #879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Python SDK v2 server examples (FastAPI, Flask) #879
Conversation
|
@Ilevk is attempting to deploy a commit to the Coinbase Team on Vercel. A member of the Team first needs to authorize it. |
|
@phdargen Hi, I hope you're doing well. Note: The examples require removing the following line from python/x402/pyproject.toml: by the way I found an issue while testing with the example servers: x402HTTPAdapter doesn't retry correctly when calling multiple different paid endpoints sequentially in the same session. The first paid endpoint works (402 → retry → 200), but subsequent different endpoints return 402 without retry. That's the reason why I've used x402HttpxClient for the test client code, which works correctly in the same scenario. |
|
I forgot to add transactions that I tested.
|
|
Hi @Ilevk, thanks a lot for your contribution! Could you please rebase against coinbase:feat/python-v2-sdk? This should fix the failing workflow checks and the problem with the extensions in python/x402/pyproject.toml |
|
It's great that you created the test_client.py scripts to test your own implementation but these should not be committed. Each server example should only include server related code. The servers can then be tested by running any of the client examples (including typescript/go) |
Good catch, this seems to be the same issue reported here #399. So this bug was already in v1. Yes, would be great if you could look into it! When you have a fix ready, please open a new PR for this targeting coinbase:feat/python-v2-sdk |
7d6bb50 to
a348d65
Compare
|
@phdargen I've just done rebase and removing And could you set me as an assignee for this PR? |
You mean that PR 399 would be closed soon?, I'd love to take a look that issue. Should I create a new Issue about that with referencing original issue or do you want me to just continue with this Issue(#398)? |
| @@ -0,0 +1 @@ | |||
| ADDRESS= | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest renaming to EVM_ADDRESS in anticipation of svm mechanism to be added soon and adding facilitator URL
| ADDRESS= | |
| EVM_ADDRESS= | |
| FACILITATOR_URL=https://x402.org/facilitator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated files here 7e1f56a
| name = "x402-v2-flask-example" | ||
| version = "0.1.0" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what name and version convention we should use here, will check with @CarsonRoscoe once he is back
| version = "0.1.0" | ||
| description = "Example of using a Flask server with x402 middleware" | ||
| readme = "README.md" | ||
| license = { text = "MIT" } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x402 uses Apache-2.0, not MIT. We don't have license in other examples, maybe better to just remove
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed it here 0ff914c
| classifiers = [ | ||
| "Development Status :: 3 - Alpha", | ||
| "Intended Audience :: Developers", | ||
| "License :: OSI Approved :: MIT License", | ||
| "Programming Language :: Python :: 3", | ||
| "Programming Language :: Python :: 3.10", | ||
| "Programming Language :: Python :: 3.11", | ||
| "Programming Language :: Python :: 3.12", | ||
| "Programming Language :: Python :: 3.13", | ||
| "Typing :: Typed", | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think classifiers is not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've removed it here 0ff914c
| accepts=[ | ||
| PaymentOption( | ||
| scheme="exact", | ||
| pay_to=ADDRESS, | ||
| price="$0.01", | ||
| network=NETWORK, | ||
| ) | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest to add description and mime_type to each endpoint
| accepts=[ | |
| PaymentOption( | |
| scheme="exact", | |
| pay_to=ADDRESS, | |
| price="$0.01", | |
| network=NETWORK, | |
| ) | |
| ] | |
| accepts=[ | |
| PaymentOption( | |
| scheme="exact", | |
| pay_to=ADDRESS, | |
| price="$0.01", | |
| network=NETWORK, | |
| ) | |
| ] | |
| description="Weather report", | |
| mime_type="application/json", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added descriptions and mime_type, here d905e0b
|
Thanks @Ilevk, the examples look good! Just a few minor comments, see above |
|
Once the svm mechanism is ready, we might want to add a solana |
|
For typescript v2 we also have advanced examples that show usage of bazaar (not yet implemented for python), hooks, dynamic price/payTo and custom money definition. Would be nice to add this for python as well (as much as currently possible), you can pick either fastapi or flask to implement this as you prefer |
|
Another thing would be a custom example that demonstrates how to implement x402 payment handling manually without using pre-built middleware packages |
PR 399 is quite old and seems inactive, so likely we will have to close it. In any case, PR 399 targets the legacy implementation. The most important thing is that we fix it for the new v2 implementation. So as a first step, I would suggest you open a new PR targeting the coinbase:feat/python-v2-sdk branch with the fix for the v2 implementation only. We can then decide later if and how we fix the legacy implementation as well |
738726b to
c26875c
Compare
c26875c to
7e1f56a
Compare
|
Let me check the other comments tomorrow. thanks for comments Happy new year!
|
|
Hi @phdargen, I've been thinking about an advanced example. I'd be nice to contribute. but should I put together same PR?, actually I wouldn't like to keep this PR in review. so why don't we finish this PR first, after that I'll create a new PR for advanced examples with new issue. |
|
Agreed @Ilevk, let's get this in first. Would also be useful for me to have the basic server example in the feat/python-v2-sdk branch to do some manual testing Please open a new PR for the advanced and/or custom examples when you are ready, no need to open new issues. |
|
Merged and updated the TODO list @Ilevk: #841 (comment) Note that I also added a new TODO item for fixing the x402HTTPAdapter retry logic (as discussed above) and assigned it to you. No problem at all if you are too busy or would rather take on something else, just let me know and I am happy to assign myself |
|
@phdargen I’ll first take a look at the advanced example, and then I’ll check the x402HTTPAdapter case. and I've updated my TODO list as well in 841 PR I really appreciate it. |
Description
Add FastAPI and Flask example servers demonstrating x402 payment middleware integration.
FastAPI (examples/python/servers/fastapi/)
Flask (examples/python/servers/flask/)
Both examples include three endpoints:
Tests
Manually tested both servers with the included test clients:
Flask example tested with identical results.
Linting passed for both:
Checklist