Package: marzban-api-client
marzban-api-clientA client library for accessing MarzbanAPITable of ContentsInstallationGetting StartedUsageContributingLicenseInstallationYou can install this library using pip:pipinstallmarzban-api-clientGetting StartedBefore you can start using this library, you need to obtain an access token by logging in.
use the following code to get access token:frommarzban_api_clientimportClientfrommarzban_api_client.api.adminimportlogin_for_access_tokenfrommarzban_api_client.models.body_login_for_access_tokenimport(BodyLoginForAccessToken,)frommarzban_api_client.models.tokenimportTokenfrommarzban_api_client.typesimportResponselogin_data=BodyLoginForAccessToken(username="USERNAME",password="PASSWORD",)client=Client(base_url="BASE_URL")withclientasclient:token:Token=login_for_access_token.sync(client=client,form_data=login_data,)access_token=token.access_token# or if you need more info (e.g. status_code)# response: Response[Token] = login_for_access_token.sync_detailed(#     client=client,#     form_data=login_data,# )ReplaceBASE_URL,USERNAME, andPASSWORDwith the actual URL of your Marzban API, your username and your password.
After executing this code, you will have anaccess_tokenthat you can use for authenticated requests.UsageNow that you have obtained an access token, you can use theAuthenticatedClientto make authenticated API requests.
Here's an example of how to use it:frommarzban_api_clientimportAuthenticatedClientfrommarzban_api_client.api.user_templateimportadd_user_templatefrommarzban_api_client.models.user_template_createimportUserTemplateCreatefrommarzban_api_client.models.user_template_responseimportUserTemplateResponseuser_template=UserTemplateCreate(name="template_1",data_limit=320000000000,expire_duration=2592000,username_prefix="USER_",)client=AuthenticatedClient(base_url="BASE_URL",token="ACCESS_TOKEN",)withclientasclient:response:UserTemplateResponse=add_user_template.sync(client=client,json_body=user_template,)print(response)ReplaceBASE_URLandACCESS_TOKENwith the actual URL of your Marzban API and and access_token.
you can also do the same thing with an async version:importasynciofrommarzban_api_clientimportAuthenticatedClientfrommarzban_api_client.api.user_templateimportadd_user_templatefrommarzban_api_client.models.user_template_createimportUserTemplateCreatefrommarzban_api_client.models.user_template_responseimportUserTemplateResponseasyncdefmain():user_template=UserTemplateCreate(name="template_1",data_limit=320000000000,expire_duration=2592000,username_prefix="USER_",)client=AuthenticatedClient(base_url="BASE_URL",token="ACCESS_TOKEN",)asyncwithclientasclient:response:UserTemplateResponse=awaitadd_user_template.asyncio(client=client,json_body=user_template,)print(response)if__name__=="__main__":asyncio.run(main())By default, when you're calling an HTTPS API it will attempt to verify that SSL is working correctly. Using certificate verification is highly recommended most of the time, but sometimes you may need to authenticate to a server (especially an internal server) using a custom certificate bundle.client=AuthenticatedClient(base_url="https://internal_api.example.com",token="SuperSecretToken",verify_ssl="/path/to/certificate_bundle.pem",)You can also disable certificate validation altogether, but beware thatthis is a security risk.client=AuthenticatedClient(base_url="https://internal_api.example.com",token="SuperSecretToken",verify_ssl=False)Things to know:Every path/method combo becomes a Python module with four functions:sync: Blocking request that returns parsed data (if successful) orNonesync_detailed: Blocking request that always returns aRequest, optionally withparsedset if the request was successful.asyncio: Likesyncbut async instead of blockingasyncio_detailed: Likesync_detailedbut async instead of blockingAll path/query params, and bodies become method arguments.Advanced customizationsThere are more settings on the generatedClientclass which let you control more runtime behavior, check out the docstring on that class for more info. You can also customize the underlyinghttpx.Clientorhttpx.AsyncClient(depending on your use-case):frommarzban_api_clientimportClientdeflog_request(request):print(f"Request event hook:{request.method}{request.url}- Waiting for response")deflog_response(response):request=response.requestprint(f"Response event hook:{request.method}{request.url}- Status{response.status_code}")client=Client(base_url="https://api.example.com",httpx_args={"event_hooks":{"request":[log_request],"response":[log_response]}},)# Or get the underlying httpx client to modify directly with client.get_httpx_client() or client.get_async_httpx_client()You can even set the httpx client directly, but beware that this will override any existing settings (e.g., base_url):importhttpxfrommarzban_api_clientimportClientclient=Client(base_url="https://api.example.com",)# Note that base_url needs to be re-set, as would any shared cookies, headers, etc.client.set_httpx_client(httpx.Client(base_url="https://api.example.com",proxies="http://localhost:8030"))ExamplesYou can find more usage examples in theexamplefolder. These examples cover various scenarios and use cases to help you get started with this library.Feel free to explore the examples and adapt them to your own project requirements.ContributingContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make aregreatly appreciated.If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!Fork the ProjectCreate your Feature Branch (git checkout -b feature/AmazingFeature)Commit your Changes (git commit -m 'Add some AmazingFeature')Push to the Branch (git push origin feature/AmazingFeature)Open a Pull RequestLicenseDistributed under the MIT License. SeeLICENSEfor more information.