We are pleased to announce that the OneSignal Rust API Client Library is now available. This new Rust client supports developers using OneSignal by facilitating integrating the OneSignal REST API with your backend events, data, and more. This release is part of our ongoing initiative to add server SDKs across various languages and technologies.

Rust is considered to be one of the most powerful and versatile modern programming languages and we're happy to support it! We're relentlessly working towards our goal to move all our services and technologies to Rust.

What is this library?

OneSignal Rust API is a faster way to get started using OneSignal REST API that is accessible as a REST service.

This library has been generated using OpenApi Generator project with several modifications and fixes on our side.

Installation

Put the package under your project folder in a directory named onesignal and add the following to Cargo.toml under [dependencies]:

onesignal = { path = "./onesignal" }

Examples: Send a notification

Use the following dependency:

use onesignal::*;
use onesignal::apis::configuration::Configuration;
use onesignal::models::{Notification};

Define the constants. You can find all the values on the dashboard page of your app.

const APP_ID: &str = "<YOUR_APP_ID>";
const APP_KEY_TOKEN: &str = "<YOUR_APP_KEY_TOKEN>";
const USER_KEY_TOKEN: &str = "<YOUR_USER_KEY_TOKEN>";

Create a configuration object:

fn create_configuration() -> Box<Configuration> {
    let mut configuration = apis::configuration::Configuration::new();
    configuration.app_key_token = Some(String::from(APP_KEY_TOKEN));
    configuration.user_key_token = Some(String::from(USER_KEY_TOKEN));
    Box::new(configuration)
}

Create a notification object:

fn create_notification() -> Box<Notification> {
    let mut notification = Notification::new(String::from(APP_ID));
    let mut string_map = StringMap::new();

    string_map.en = Some(String::from("Rust test notification"));
    notification.contents = Some(Box::new(string_map));
    notification.is_chrome_web = Some(true);
    notification.is_any_web = Some(true);
    notification.included_segments = Some(vec![String::from("Subscribed Users")]);

    Box::new(notification)
}

Send a notification:

async fn should_create_notification() {
    // Prepare configuration and the notification objects
    let configuration = create_configuration();
    let notification = create_notification();

    // Send notification to the server
    let create_notification_response= apis::default_api::create_notification(&configuration, *notification).await;

    // Check the result
    if let Ok(ref created_notification) = create_notification_response {
        println!("Created notification id: {}", created_notification.id);
    }

    if let Err(ref created_notification_error) = create_notification_response {
        println!("Created notification error: {}", created_notification_error.to_string());
    }
}

Documentation for API Endpoints

All URIs are relative to https://onesignal.com/api/v1

Class Method HTTP request Description
DefaultApi cancel_notification DELETE /notifications/{notification_id} Stop a scheduled or currently outgoing notification
DefaultApi create_app POST /apps Create an app
DefaultApi create_notification POST /notifications Create notification
DefaultApi create_player POST /players Add a device
DefaultApi create_segments POST /apps/{app_id}/segments Create Segments
DefaultApi delete_player DELETE /players/{player_id} Delete a user record
DefaultApi delete_segments DELETE /apps/{app_id}/segments/{segment_id} Delete Segments
DefaultApi export_players POST /players/csv_export?app_id={app_id} CSV export
DefaultApi get_app GET /apps/{app_id} View an app
DefaultApi get_apps GET /apps View apps
DefaultApi get_notification GET /notifications/{notification_id} View notification
DefaultApi get_notification_history POST /notifications/{notification_id}/history Notification History
DefaultApi get_notifications GET /notifications View notifications
DefaultApi get_outcomes GET /apps/{app_id}/outcomes View Outcomes
DefaultApi get_player GET /players/{player_id} View device
DefaultApi get_players GET /players View devices
DefaultApi update_app PUT /apps/{app_id} Update an app
DefaultApi update_player PUT /players/{player_id} Edit device
DefaultApi update_player_tags PUT /apps/{app_id}/users/{external_user_id} Edit tags with external user id

Documentation For Models

To get access to the crate's generated documentation, use:

cargo doc --open

You can find more examples in our GitHub repo!

Reference

Visit our REST API Reference documentation for a complete list of our supported endpoints.

Visit our Rust API reference for a list of supported functions and view implementation examples in Rust.

Share Your Feedback

If you’ve had a chance to use our new Rust API Library and have any feedback or suggestions you’d like to share, please reach out to us on the GitHub repository or our Discord server.

If you encounter any bugs or technical issues while using our new Rust API Library, we want to know so we can make things right. Please be sure to file a report on our GitHub repository.

If you’ve found the plugin helpful, we’d appreciate it if you left us a review on G2.