Flutter WP JSON API Documentation

Introduction


Welcome to the documentation for WP JSON API for Flutter!
Here you'll be able to learn how it works and explore the features available.
If you have any questions please feel free to contact us

Requirements


To quickly get set up, you'll need to ensure that you have the below requirements.

  • WordPress 4.0+

    Woocommerce Store 3.0+ (Optional)

    Flutter

Installing the Plugin


Follow the below steps to install the plugin on your WordPress site:

  • Login into your WooSignal account (or create an account if you are new).

  • Once you reach the Dashboard, on the left nav bar look for "Plugins" and click the link.

  • After downloading the plugin, visit your WordPress site and login to get to your admin dashboard. On your nav bar, look for "Plugins" like the below image.

    Select "Add New" and proceed to the next step below.

  • Then select "Upload plugin" like the below image

    This will open a box below where you can upload a .zip file.

  • Click "Choose file" can look for the WooSignal plugin you downloaded and select the .zip file to install. After you select "Install Now". You should then be able to activate the plugin from your WordPress admin to start using it.

  • If you followed all the steps above you should now be ready to start using the plugin in Flutter.

  • Next

    Install the Flutter package

    Add the following line to your pubspec.yaml file

    
    dependencies:
      wp_json_api: ^3.3.2
                                            

    2. Install it

    You can install packages from the command line:

    $ flutter pub get
    

Error handling

If the request payload "status" returns a value higher than 200 then an error has happened.
When an error occurs with an API request, the expected object response will return as null.


Initializing WPJson in Flutter

In your main.dart file create a new instance of WPJsonAPI like the following example below

import 'package:woosignal/wp_json_api.dart';
...

void main() {

WPJsonAPI.instance.initWith(baseUrl: "https://mysite.com");

...

Get Nonce

Returns a nonce token from WordPress, use the response token for logging in and registering new users.

Method

WPNonceResponse wpNonceResponse = await WPJsonAPI.instance.api((request) {
 return request.wpNonce();
});

Returns WPNonceResponse | null

Data data;
    String nonce;
    String root;
    int expiry;

String message;
int status;

Verify Nonce

Returns the bool value if a nonce is still valid to use.

Method

WPNonceVerifiedResponse wpNonceVerifiedResponse = await WPJsonAPI.instance.api((request) {
 return request.wpNonceVerify(
      nonce: "nonce token"
      );
});

Body Parameters


wpNonceVerify(
    nonce: "string (required)"
);

Returns WPNonceVerifiedResponse | null

Data data;
    bool isValid;

String message;
int status;

Register a user

Register a user on WordPress

Method

WPUserRegisterResponse wpUserRegisterResponse = await WPJsonAPI.instance.api((request) {
 return request.wpRegister(
      email: "wpuser@gmail.com",
      password: "thepassword2",
      username: "usernamewp"
      );
});

Body Parameters


wpRegister(
    email: "wpuser@gmail.com",
    password: "thepassword2",
    username: "usernamewp",
    expiry: "(optional for when you want the response token to expire in e.g. +1 day)"
);

Returns WPUserRegisterResponse | null

Data data;
    String userToken;
    int expiry;
    // expiry is a timestamp for when the userToken will expire
    // if no expiry was passed in this will be 0 which means it will never expire

String message;
int status;

WordPress Login

Login as a user on WordPress, this returns a userToken that you can use for future requests.

Method

WPUserLoginResponse wpUserLoginResponse = await WPJsonAPI.instance.api((request) {
 return request.wpLogin(
    email: "wpuser@gmail.com",
      password: "thepassword");
});

Body Parameters


wpLogin(
    String email,
    String username,
    required String password,
    WPAuthType authType = WPAuthType.WpEmail,
    String tokenExpiryAt

    // Note: authType enum
    // WPAuthType.WpEmail will attempt to authenticate with the email and password
    // WPAuthType.WpUsername will attempt to authenticate with the username and password

    // Note: tokenExpiryAt
    // Set when you want the response token to expire in x amount of time e.g. +1 day
);

Returns WPUserLoginResponse | null

Data data;
    String userToken;
    int expiry;

String message;
int status;

WordPress Users Info

Returns a users info from WordPress

Method

WPUserInfoResponse wpUserInfoResponse = await WPJsonAPI.instance.api((request) {
 return request.wpGetUserInfo(
    "userToken (from login/register request)"
 );
});

Returns WPUserInfoResponse | null

Data data;
    int id;
    String firstName;
    String lastName;
    String username;
    String email;
    String avatar;
    String createdAt;

String message;
int status;

Update Users Info

Update an authenticated users info

Method

WPUserInfoUpdatedResponse wpUserInfoUpdatedResponse = await WPJsonAPI.instance.api((request) {
 return request(
    "userToken (from login/register request)",
      firstName: "first name",
      lastName: "last name",
      displayName: "display name"
);
});

Returns WPUserInfoUpdatedResponse | null

String message;
int status;

Update Users Password

Update an authenticated users password

Method

WPUserResetPasswordResponse wpUserResetPasswordResponse = await WPJsonAPI.instance.api((request) {
 return request.wpResetPassword(
    userToken,
    password: "new password",
);
});

Body Parameters

wpResetPassword(
    "userToken (from login/register request)",
    password: "new password",
);

Returns WPUserResetPasswordResponse | null

List data
String message;
int status;

API Requests: WooCommerce

WooCommerce Get Users Info

Returns a customers info from WooCommerce, we include the billing and shipping

Method

WCCustomerInfoResponse wcCustomerInfoResponse = await WPJsonAPI.instance.api((request) {
 return request.wcCustomerInfo(
    "userToken (from login/register request)"
 );
});

Returns WCCustomerInfoResponse | null

Data data;
    String firstName;
    String lastName;
    String displayName;
    String avatar;
    Shipping shipping;
        String firstName;
        String lastName;
        String company;
        String address1;
        String address2;
        String city;
        String state;
        String postcode;
        String country;
    Billing billing;
        String firstName;
        String lastName;
        String company;
        String address1;
        String address2;
        String city;
        String state;
        String postcode;
        String country;
        String email;
        String phone;

String message;
int status;

WooCommerce Update Users Info

Send the fields you want to update and the plugin will handle the rest

Method

WCCustomerUpdatedResponse wcCustomerUpdatedResponse = await WPJsonAPI.instance.api((request) {
 return request.wcUpdateCustomerInfo(
    "userToken (from login/register request)",
      firstName: "firstName value",
      lastName: "lastName value",
      displayName: "displayName value",
      billingFirstName: "billingFirstName value",
      billingLastName: "billingLastName value",
      billingCompany: "billingCompany value",
      billingAddress1: "billingAddress1 value",
      billingAddress2: "billingAddress2 value",
      billingCity: "billingCity value",
      billingState: "billingState value",
      billingPostcode: "billingPostcode value",
      billingCountry: "billingCountry value",
      billingEmail: "billingEmail value",
      billingPhone: "billingPhone value",
      shippingFirstName: "shippingFirstName value",
      shippingLastName: "shippingLastName value",
      shippingCompany: "shippingCompany value",
      shippingAddress1: "shippingAddress1 value",
      shippingAddress2: "shippingAddress2 value",
      shippingCity: "shippingCity value",
      shippingState: "shippingState value",
      shippingPostcode: "shippingPostcode value",
      shippingCountry: "shippingCountry value",
      shippingEmail: "shippingEmail value",
      shippingPhone: "shippingPhone value"
);
});

Body Parameters


wcUpdateCustomerInfo(
    "userToken (from login/register request)",
      firstName: "firstName value",
      lastName: "lastName value",
      displayName: "displayName value",
      billingFirstName: "billingFirstName value",
      billingLastName: "billingLastName value",
      billingCompany: "billingCompany value",
      billingAddress1: "billingAddress1 value",
      billingAddress2: "billingAddress2 value",
      billingCity: "billingCity value",
      billingState: "billingState value",
      billingPostcode: "billingPostcode value",
      billingCountry: "billingCountry value",
      billingEmail: "billingEmail value",
      billingPhone: "billingPhone value",
      shippingFirstName: "shippingFirstName value",
      shippingLastName: "shippingLastName value",
      shippingCompany: "shippingCompany value",
      shippingAddress1: "shippingAddress1 value",
      shippingAddress2: "shippingAddress2 value",
      shippingCity: "shippingCity value",
      shippingState: "shippingState value",
      shippingPostcode: "shippingPostcode value",
      shippingCountry: "shippingCountry value",
      shippingEmail: "shippingEmail value",
      shippingPhone: "shippingPhone value"
);

Returns WCCustomerUpdatedResponse | null

List<dynamic> data
String message;
int status;