{"id":20215581,"url":"https://github.com/c12i/mpesa-rust","last_synced_at":"2025-06-26T14:31:44.770Z","repository":{"id":45166644,"uuid":"279136176","full_name":"c12i/mpesa-rust","owner":"c12i","description":"A MPESA API sdk in Rust","archived":false,"fork":false,"pushed_at":"2024-07-08T15:31:52.000Z","size":35728,"stargazers_count":42,"open_issues_count":16,"forks_count":15,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-14T12:05:14.246Z","etag":null,"topics":["mpesa","rust","safaricom"],"latest_commit_sha":null,"homepage":"https://c12i.github.io/mpesa-rust/mpesa/index.html","language":"Rust","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/c12i.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","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":"2020-07-12T19:43:59.000Z","updated_at":"2025-05-14T16:44:19.000Z","dependencies_parsed_at":"2023-11-24T09:29:02.703Z","dependency_job_id":"b012b544-52ba-4ba4-a9ad-3f433669229a","html_url":"https://github.com/c12i/mpesa-rust","commit_stats":{"total_commits":374,"total_committers":3,"mean_commits":"124.66666666666667","dds":0.4331550802139037,"last_synced_commit":"3d78a3304cbfad0a1f7d9d1c88b9a96243e85ac6"},"previous_names":["c12i/mpesa-rust","collinsmuriuki/mpesa-rust"],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/c12i/mpesa-rust","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fmpesa-rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fmpesa-rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fmpesa-rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fmpesa-rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c12i","download_url":"https://codeload.github.com/c12i/mpesa-rust/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c12i%2Fmpesa-rust/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262084602,"owners_count":23256266,"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":["mpesa","rust","safaricom"],"created_at":"2024-11-14T06:23:22.591Z","updated_at":"2025-06-26T14:31:44.677Z","avatar_url":"https://github.com/c12i.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mpesa-rust\n\n[![Rust](https://github.com/c12i/mpesa-rust/actions/workflows/general.yml/badge.svg?branch=master)](https://github.com/c12i/mpesa-rust/actions/workflows/general.yml)\n[![](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)\n[![](https://img.shields.io/crates/v/mpesa)](https://crates.io/crates/mpesa)\n\n[![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?style=for-the-badge\u0026logo=discord\u0026logoColor=white)](https://discord.gg/xswEKrrVGE)\n\n## About\n\nAn unofficial Rust wrapper around the [Safaricom API](https://developer.safaricom.co.ke/docs?shell#introduction) for accessing M-Pesa services.\n\n## Install\n\n`Cargo.toml`\n\n```toml\n[dependencies]\nmpesa = { version = \"1\" }\n```\n\nOptionally, you can disable default-features, which is basically the entire suite of MPESA APIs to conditionally select individual features. (See [Services](#services) table for the full list of Cargo features)\n\nExample:\n\n```toml\n[dependencies]\nmpesa = { version = \"1\", default_features = false, features = [\"b2b\", \"express_request\"] }\n```\n\nIn your lib or binary crate:\n\n```rust\nuse mpesa::Mpesa;\n```\n\n## Usage\n\n### Creating a `Mpesa` client\n\nYou will first need to create an instance of the `Mpesa` instance (the client). You are required to provide a **CONSUMER_KEY** and\n**CONSUMER_SECRET**. [Here](https://developer.safaricom.co.ke/test_credentials) is how you can get these credentials for the Safaricom sandbox\nenvironment. It's worth noting that these credentials are only valid in the sandbox environment. To go live and get production keys\nread the docs [here](https://developer.safaricom.co.ke/docs?javascript#going-live).\n\nThese are the following ways you can instantiate `Mpesa`:\n\n```rust\nuse mpesa::{Mpesa, Environment};\n\n#[tokio::main]\nasync fn main() {\n    dotenvy::dotenv().ok();\n\n    let client = Mpesa::new(\n        dotenvy::var(\"CONSUMER_KEY\").unwrap(),\n        dotenvy::var(\"CONSUMER_SECRET\").unwrap(),\n        Environment::Sandbox,\n    );\n\n    assert!(client.is_connected().await);\n}\n```\n\nSince the `Environment` enum implements `FromStr` and `TryFrom` for `String` and `\u0026str` types, you can call `Environment::from_str` or `Environment::try_from` to create an `Environment` type. This is ideal if the environment values are\nstored in a `.env` or any other configuration file:\n\n```rust\nuse mpesa::{Mpesa, Environment};\nuse std::convert::TryFrom;\nuse std::error::Error;\nuse std::str::FromStr;\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), Box\u003cdyn Error\u003e\u003e {\n    dotenvy::dotenv().ok();\n\n    let client = Mpesa::new(\n        dotenvy::var(\"CONSUMER_KEY\").unwrap(),\n        dotenvy::var(\"CONSUMER_SECRET\").unwrap(),\n        Environment::from_str(\"sandbox\")?, // or\n        // Environment::try_from(\"sandbox\")?,\n    );\n\n    assert!(client.is_connected().await);\n    Ok(())\n}\n```\n\nThe `Mpesa` struct's `environment` parameter is generic over any type that implements the `ApiEnvironment` trait. This trait\nexpects the following methods to be implemented for a given type:\n\n```rust\npub trait ApiEnvironment {\n    fn base_url(\u0026self) -\u003e \u0026str;\n    fn get_certificate(\u0026self) -\u003e \u0026str;\n}\n```\n\nThis trait allows you to create your own type to pass to the `environment` parameter. With this in place, you are able to mock http requests (for testing purposes) from the MPESA api by returning a mock server uri from the `base_url` method as well as using your own certificates, required to sign select requests to the MPESA api, by providing your own `get_certificate` implementation.\n\nSee the example below (and [here](./src/environment.rs) so see how the trait is implemented for the `Environment` enum):\n\n```rust\nuse mpesa::{Mpesa, ApiEnvironment};\n\n#[derive(Clone)]\npub struct CustomEnvironment;\n\nimpl ApiEnvironment for CustomEnvironment {\n    fn base_url(\u0026self) -\u003e \u0026str {\n        // your base url here\n        \"https://your_base_url.com\"\n    }\n\n    fn get_certificate(\u0026self) -\u003e \u0026str {\n        // your certificate here\n        r#\"...\"#\n    }\n}\n\n#[tokio::main]\nasync fn main() {\n    dotenvy::dotenv().ok();\n\n    let client = Mpesa::new(\n        dotenvy::var(\"CONSUMER_KEY\").unwrap(),\n        dotenvy::var(\"CONSUMER_SECRET\").unwrap(),\n        CustomEnvironment,\n    );\n}\n```\n\nIf you intend to use in production, you will need to call a the `set_initiator_password` method from `Mpesa` after initially\ncreating the client. Here you provide your initiator password, which overrides the default password used in sandbox `\"Safcom496!\"`:\n\n```rust\nuse mpesa::{Mpesa, Environment};\n\n#[tokio::main]\nasync fn main() {\n    dotenvy::dotenv().ok();\n\n    let client = Mpesa::new(\n        dotenvy::var(\"CONSUMER_KEY\").unwrap(),\n        dotenvy::var(\"CONSUMER_SECRET\").unwrap(),\n        Environment::Sandbox,\n    );\n\n    client.set_initiator_password(\"new_password\");\n    assert!(client.is_connected().await)\n}\n```\n\n### Services\n\nThe table below shows all the MPESA APIs from Safaricom and those supported by the crate along with their cargo features and usage examples\n\n| API                                                                                                         | Cargo Feature          | Status          | Example                                                              |\n| ----------------------------------------------------------------------------------------------------------- | ---------------------- | --------------- | -------------------------------------------------------------------- |\n| [Account Balance](https://developer.safaricom.co.ke/APIs/AccountBalance)                                    | `account_balance`      | Stable ✅       | [account balance example](/docs/client/account_balance.md)           |\n| [B2B Express Checkout](https://developer.safaricom.co.ke/APIs/B2BExpressCheckout)                           | N/A                    | Unimplemented   | N/A                                                                  |\n| [Bill Manager](https://developer.safaricom.co.ke/APIs/BillManager)                                          | `bill_manager`         | Unstable ⚠️     | [bill manager examples](/docs/client/bill_manager/)                  |\n| [Business Buy Goods](https://developer.safaricom.co.ke/APIs/BusinessBuyGoods)                               | `b2b`                  | Stable ✅       | [business buy goods example](/docs/client/b2b.md)                    |\n| [Business Pay Bill](https://developer.safaricom.co.ke/APIs/BusinessPayBill)                                 | N/A                    | Unimplemented   | N/A                                                                  |\n| [Business To Customer (B2C)](https://developer.safaricom.co.ke/APIs/BusinessToCustomer)                     | `b2c`                  | Stable ✅️      | [b2c example](/docs/client/b2c.md)                                   |\n| [Customer To Business (Register URL)](https://developer.safaricom.co.ke/APIs/CustomerToBusinessRegisterURL) | `c2b_register`         | Stable ✅️      | [c2b register example](/docs/client/c2b_register.md)                 |\n| [Customer To Business (Simulate)](#)                                                                        | `c2b_simulate`         | Stable ✅️      | [c2b simulate example](/docs/client/c2b_simulate.md)                 |\n| [Dynamic QR](https://developer.safaricom.co.ke/APIs/DynamicQRCode)                                          | `dynamic_qr`           | Stable ✅️      | [dynamic qr example](/docs/client/dynamic_qr.md)                     |\n| [M-PESA Express (Query)](https://developer.safaricom.co.ke/APIs/MpesaExpressQuery)                          | `express`                    | Stable ✅️           ️ | [express query example](/docs/client/express.md)                                                                         |\n| [M-PESA Express (Simulate)/ STK push](https://developer.safaricom.co.ke/APIs/MpesaExpressSimulate)          | `express`      | Stable ✅️      | [express request example](/docs/client/express.md)           |\n| [Transaction Status](https://developer.safaricom.co.ke/APIs/TransactionStatus)                              | `transaction_status`   | Stable ✅️      | [transaction status example](/docs/client/transaction_status.md)     |\n| [Transaction Reversal](https://developer.safaricom.co.ke/APIs/Reversal)                                     | `transaction_reversal` | Stable ✅️      | [transaction reversal example](/docs/client/transaction_reversal.md) |\n| [Tax Remittance](https://developer.safaricom.co.ke/APIs/TaxRemittance)                                      | N/A                    | Unimplemented   | N/A                                                          |\n\n## Author\n\n**Collins Muriuki**\n\n- Twitter: [@c12i\\_](https://twitter.com/c12i_)\n- Not affiliated with Safaricom.\n\n## Contributing\n\nContributions, issues and feature requests are welcome!\u003cbr /\u003eFeel free to check [issues page](https://github.com/collinsmuriuki/mpesa-rust/issues). You can also take a look at the [contributing guide](https://raw.githubusercontent.com/collinsmuriuki/mpesa-rust/master/CONTRIBUTING.md).\n\n\u003ca href=\"https://github.com/c12i/mpesa-rust/graphs/contributors\"\u003e\n  \u003cimg src=\"https://contrib.rocks/image?repo=c12i/mpesa-rust\" /\u003e\n\u003c/a\u003e\n\nMade with [contrib.rocks](https://contrib.rocks).\n\n---\n\nCopyright © 2023 [Collins Muriuki](https://github.com/collinsmuriuki).\u003cbr /\u003e\nThis project is [MIT](https://raw.githubusercontent.com/collinsmuriuki/mpesa-rust/master/LICENSE) licensed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc12i%2Fmpesa-rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc12i%2Fmpesa-rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc12i%2Fmpesa-rust/lists"}