Crate twitch_api_rs[][src] Gihub Actions Testing CI

A twitch crate used to build and send requests to the twitch helix api.

See Twitch API Reference

Example: Getting the top 20 clips from a creator

To get a list of clips from the twitch api, by a streamers dislplay name, there are 4 steps.

  1. Get an application ClientId and ClientSecret 1
  2. Request a client_flow Authentication Token
  3. Use that token to request information on a user by their display name
  4. Use the UserId returned by that request to request a list of clips associated with their channel.
async fn main() {
    use twitch_api_rs::prelude::*;
    use twitch_api_rs::auth::{ClientId, ClientSecret};

    // Here we use the values that you should have gotten from step one

    let client_id: ClientId =
            .expect("Client Id environment variable not defined")

    let client_secret: ClientSecret =
            .expect("Client Secret environment variable not defined")

    // Make a reqwest client to send the requests, and wrap it in an arc so it
    // can be reused in multiple futures
    let client = Arc::new(reqwest::Client::new());

    use twitch_api_rs::auth::client_credentials::{
        ClientAuthRequest, ClientAuthResponse, ClientAuthToken,

    // Step 2

    // Get a client credentials (application) access token and wrap it in an arc
    // to be used across multiple tasks without repeating
    let auth_token: Arc<ClientAuthToken> = Arc::new(
        match ClientAuthRequest::builder()
            .await {
                Ok(resp) => {
                    // Create the token from the token value provided by twitch and
                    // your client_id
                    ClientAuthToken::from_client(resp, client_id)

                // Better error handling can be performed here by matching against
                // the type of this requests::RequestError. Elided for conciseness
                Err(e) => panic!("Could not complete auth request for reason {}", e),

    // Step 3

    use twitch_api_rs::values::users::UserId;
    use twitch_api_rs::resource::users::get_users::*;

    // Notice that we get a Vec<UserId> here, as this endpoint allows you to query
    // for multiple users at once
    let user_ids: Vec<UserId> =
        match GetUsersRequest::builder()
            .await {
                Ok(mut resp) => {

                Err(e) =>
                    panic!("Could not complete request for user by display name for reason {}", e),

    // Step 4

    use twitch_api_rs::resource::clips::ClipInfo;
    use twitch_api_rs::resource::clips::get_clips::*;
    use twitch_api_rs::values::clips::ClipTitle;

    for user_id in user_ids {
        let auth_token = auth_token.clone();
        let client = client.clone();

        tokio::spawn(async move {
            match GetClipsRequest::builder()
                .await {
                    Ok(resp) => {
                        // Print out the response
                        for clip in resp.clips {
                                "Found clip for broadcaster {:?} titled {:?}",
                                &user_id, clip.title
                    Err(e) =>
                        panic!("Could not get clips for user {:?} for reason {}", user_id, e),

  1. If you do not already have these then you will need to log into your twitch developer console, create and name a new application, and copy down the associated values.
    The client secret that you get from this process is imediately invalidated if you request a new one, and it will not be shown to you once you leave the page where you generate it, so make sure to write it down somewhere safe. It is recommended that you treat it like a password. 



Types and functions for auth flows


Common functions and types used in most application


Common traits used to construct requestable types


All endpoints organized by which resource they are in relation to.
See the API Reference for a list of all resources.


Wrappers for basic owned types that indicate their usage



Create identity functions for Eq types


Generate a ErrorCodes impl block for a given Enum by mapping known status codes to specific variants. Variants must not be struct variants