{"id":21659679,"url":"https://github.com/dongri/openai-api-rs","last_synced_at":"2025-12-29T12:13:30.074Z","repository":{"id":78497095,"uuid":"577126069","full_name":"dongri/openai-api-rs","owner":"dongri","description":"OpenAI API client library for Rust (unofficial)","archived":false,"fork":false,"pushed_at":"2024-04-15T07:03:14.000Z","size":136,"stargazers_count":211,"open_issues_count":1,"forks_count":37,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-05-02T06:11:30.112Z","etag":null,"topics":["api","gpt-3-5-turbo","gpt-4","openai","rust"],"latest_commit_sha":null,"homepage":"https://docs.rs/openai-api-rs","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/dongri.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":"2022-12-12T02:41:19.000Z","updated_at":"2024-05-14T05:31:23.923Z","dependencies_parsed_at":"2023-02-24T12:30:33.829Z","dependency_job_id":"4f4e474e-eaa5-41cc-88c2-b55691992ad3","html_url":"https://github.com/dongri/openai-api-rs","commit_stats":null,"previous_names":[],"tags_count":75,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dongri%2Fopenai-api-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dongri%2Fopenai-api-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dongri%2Fopenai-api-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dongri%2Fopenai-api-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dongri","download_url":"https://codeload.github.com/dongri/openai-api-rs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247135144,"owners_count":20889421,"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":["api","gpt-3-5-turbo","gpt-4","openai","rust"],"created_at":"2024-11-25T09:31:22.682Z","updated_at":"2025-12-29T12:13:30.069Z","avatar_url":"https://github.com/dongri.png","language":"Rust","funding_links":[],"categories":["Rust","CLIs"],"sub_categories":[],"readme":"# OpenAI API client library for Rust (unofficial)\n\nThe OpenAI API client Rust library provides convenient access to the OpenAI API from Rust applications.\n\nCheck out the [docs.rs](https://docs.rs/openai-api-rs/).\n\n## Installation:\n\nCargo.toml\n\n```toml\n[dependencies]\nopenai-api-rs = \"9.0.1\"\n```\n\n## Usage\n\nThe library needs to be configured with your account's secret key, which is available on the [website](https://platform.openai.com/account/api-keys). We recommend setting it as an environment variable. Here's an example of initializing the library with the API key loaded from an environment variable and creating a completion:\n\n### Set OPENAI_API_KEY or OPENROUTER_API_KEY to environment variable\n\n```bash\n$ export OPENAI_API_KEY=sk-xxxxxxx\nor\n$ export OPENROUTER_API_KEY=sk-xxxxxxx\n```\n\n### Create OpenAI client\n\n```rust\nlet api_key = env::var(\"OPENAI_API_KEY\").unwrap().to_string();\nlet mut client = OpenAIClient::builder().with_api_key(api_key).build()?;\n```\n\n### Create OpenRouter client\n\n```rust\nlet api_key = env::var(\"OPENROUTER_API_KEY\").unwrap().to_string();\nlet mut client = OpenAIClient::builder()\n    .with_endpoint(\"https://openrouter.ai/api/v1\")\n    .with_api_key(api_key)\n    .build()?;\n```\n\n### Create request\n\n```rust\nlet req = ChatCompletionRequest::new(\n    GPT4_O.to_string(),\n    vec![chat_completion::ChatCompletionMessage {\n        role: chat_completion::MessageRole::user,\n        content: chat_completion::Content::Text(String::from(\"What is bitcoin?\")),\n        name: None,\n        tool_calls: None,\n        tool_call_id: None,\n    }],\n);\n```\n\n### Send request\n\n```rust\nlet result = client.chat_completion(req)?;\nprintln!(\"Content: {:?}\", result.choices[0].message.content);\n\nfor (key, value) in client.headers.unwrap().iter() {\n    println!(\"{}: {:?}\", key, value);\n}\n```\n\n### Set OPENAI_API_BASE to environment variable (optional)\n\n```bash\n$ export OPENAI_API_BASE=https://api.openai.com/v1\n```\n\n## Example of chat completion\n\n```rust\nuse openai_api_rs::v1::api::OpenAIClient;\nuse openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};\nuse openai_api_rs::v1::common::GPT4_O;\nuse std::env;\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let api_key = env::var(\"OPENAI_API_KEY\").unwrap().to_string();\n    let mut client = OpenAIClient::builder().with_api_key(api_key).build()?;\n\n    let req = ChatCompletionRequest::new(\n        GPT4_O.to_string(),\n        vec![chat_completion::ChatCompletionMessage {\n            role: chat_completion::MessageRole::user,\n            content: chat_completion::Content::Text(String::from(\"What is bitcoin?\")),\n            name: None,\n            tool_calls: None,\n            tool_call_id: None,\n        }],\n    );\n\n    let result = client.chat_completion(req).await?;\n    println!(\"Content: {:?}\", result.choices[0].message.content);\n\n    for (key, value) in client.headers.unwrap().iter() {\n        println!(\"{}: {:?}\", key, value);\n    }\n\n    Ok(())\n}\n```\n\n## Example for OpenRouter\n\n```rust\nuse openai_api_rs::v1::api::OpenAIClient;\nuse openai_api_rs::v1::chat_completion::{self, ChatCompletionRequest};\nuse openai_api_rs::v1::common::GPT4_O_MINI;\nuse std::env;\n\n#[tokio::main]\nasync fn main() -\u003e Result\u003c(), Box\u003cdyn std::error::Error\u003e\u003e {\n    let api_key = env::var(\"OPENROUTER_API_KEY\").unwrap().to_string();\n    let mut client = OpenAIClient::builder()\n        .with_endpoint(\"https://openrouter.ai/api/v1\")\n        .with_api_key(api_key)\n        .build()?;\n\n    let req = ChatCompletionRequest::new(\n        GPT4_O_MINI.to_string(),\n        vec![chat_completion::ChatCompletionMessage {\n            role: chat_completion::MessageRole::user,\n            content: chat_completion::Content::Text(String::from(\"What is bitcoin?\")),\n            name: None,\n            tool_calls: None,\n            tool_call_id: None,\n        }],\n    );\n\n    let result = client.chat_completion(req).await?;\n    println!(\"Content: {:?}\", result.choices[0].message.content);\n\n    for (key, value) in client.headers.unwrap().iter() {\n        println!(\"{}: {:?}\", key, value);\n    }\n\n    Ok(())\n}\n```\n\nMore Examples: [examples](https://github.com/dongri/openai-api-rs/tree/main/examples)\n\nCheck out the [full API documentation](https://platform.openai.com/docs/api-reference/completions) for examples of all the available functions.\n\n## Supported APIs\n\n- [x] [Completions](https://platform.openai.com/docs/api-reference/completions)\n- [x] [Chat](https://platform.openai.com/docs/api-reference/chat)\n- [x] [Edits](https://platform.openai.com/docs/api-reference/edits)\n- [x] [Images](https://platform.openai.com/docs/api-reference/images)\n- [x] [Embeddings](https://platform.openai.com/docs/api-reference/embeddings)\n- [x] [Audio](https://platform.openai.com/docs/api-reference/audio)\n- [x] [Files](https://platform.openai.com/docs/api-reference/files)\n- [x] [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning)\n- [x] [Moderations](https://platform.openai.com/docs/api-reference/moderations)\n- [x] [Function calling](https://platform.openai.com/docs/guides/gpt/function-calling)\n- [x] [Assistants](https://platform.openai.com/docs/assistants/overview)\n- [x] [Batch](https://platform.openai.com/docs/api-reference/batch)\n- [x] [Realtime](https://platform.openai.com/docs/api-reference/realtime)\n- [x] [Responses](https://platform.openai.com/docs/api-reference/responses)\n\n## License\n\nThis project is licensed under [MIT license](https://github.com/dongri/openai-api-rs/blob/main/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdongri%2Fopenai-api-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdongri%2Fopenai-api-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdongri%2Fopenai-api-rs/lists"}