# Javascript API Methods

## Fetch reviews

See [List Reviews](https://developers.fera.ai/reference/public/tag/reviews/GET/reviews)

### List all reviews

Fetch all reviews, including store reviews and product reviews.

```javascript
fera.api.listReviews().then(({ meta, reviews }) => {
  console.log(`Found ${ meta.total_count } reviews`);
  
  reviews.forEach((review) => {
    if (review.product) {
    	console.log(`${ review.customer.get('display_name') } rated ${ review.product.get('name') } ${ review.rating }/5.`);
    } else {
    	console.log(`${ review.customer.get('display_name') } rated the store ${ review.rating }/5.`);
    }
  });
});
```

### List reviews for a specific product

List only reviews about a specific product.

```javascript
fera.api.listProductReviews('PRODUCT_ID').then(({ meta, reviews, product }) => {
  console.log(`Found ${ meta.total_count } reviews for product ${ product.get('name') }`);
  
  reviews.forEach((review) => {
    console.log(`${ review.customer.get('display_name') } rated the product ${ review.rating }/5.`);
  });
});
```

## Fetch ratings

See [List ratings](https://developers.fera.ai/reference/public/tag/ratings/GET/products/ratings)

### Retrieve product rating

```javascript
fera.api.retrieveProductRating('PRODUCT_ID').then((rating) => {
	console.log(`That product is rated ${ rating.average }/5 and has ${ rating.count } reviews`);
});
```

### List multiple product ratings

> **Tip:** Go ahead, do multiple `fera.api.retrieveProductRating`
>
> The `retrieveProductRating` method will automatically combine requests into one if placed within 1ms of each other.

```javascript

Promise.all(['PRODUCT_ID1', 'PRODUCT_ID2'].map(
  productId => fera.api.retrieveProductRating(productId))
).then((ratings) => {
  ratings.forEach((rating) => {
    console.log(`Product with ID ${ rating.get('external_product_id') } is rated ${ rating.average }/5 and has ${ rating.count } reviews`);
  });
});
```

### Retrieve store's overall rating

```javascript
fera.api.getRating().then((rating) => {
	console.log(`This store's rated ${ rating.average }/5 and has ${ rating.count } reviews`);
});
```

## Fetch photos & videos (media)

### List all photos & videos

```javascript
fera.api.listMedia().then(({ meta, media }) => {
  console.log(`Found ${ meta.total_count } photos/videos.`);
  
  media.forEach((m) => {
    console.log(`${ m.customer.get('display_name') } submitted ${ m.get('type') }: ${ m.get('url') }.`);
  });
});
```

### List photos and videos for a specific product

```javascript
fera.api.listProductMedia('PRODUCT_ID').then(({ meta, media }) => {
  console.log(`Found ${ meta.total_count } photos/videos for product.`);
  
  media.forEach((m) => {
    console.log(`${ m.customer.get('display_name') } submitted ${ m.type }: ${ m.url }.`);
  });
});
```

## Show submission modal

Show the review/photo/video submission modal.

```javascript
fera.showContentSubmitter();
```

## Push conversion event

Let Fera know that a purchase (AKA order) has occurred. This will trigger scheduling of review requests in the future.

```javascript
fera.push('order', { // FYI fera.push('pushOrder' also works, this is just more readable
  id:            "12345",
  line_items: [{
    name:         "Product 123",
    product_id:   "product-123",
    variant_id:   "variation1", // If variant exists
    total:        100.0
  }],
  customer_id:   "customer-123"
});
```

## Set current state

### Set current product

```javascript
fera.push('setProduct', {
  "id":                   "product-123", // String
  "name":                 "Product 123", // String
  "price":                99.89, // (Optional) Float
  "url":                  "https://www.example.com/products/product-123", // String
  "thumbnail_url":        "https://cdn.example.com/products/product-123/image.png" // String
});
```

> **Tip:** As long as you installed one of our site platform apps (like our Shopify App, Wix App or BigCommerce App) then this is done automatically for you.

### Set current customer

Tells Fera who the current customer that is browsing the site is so they can be prefilled into the customer review submission modal (if they didn't click on a review request link).

```javascript
fera.setCustomer({ 
  name: "Tobias Funke", 
  email: "tobias@example.com",
});
```

> **Warning:** This can be untrustworthy
>
> Consider using the [URL API](https://developers.fera.ai/link-for-customer-to-submit-review) instead to confirm that the customer email is actually owned by the current user. Setting the email from the JavaScript side of things can be faked!

## Listen to events

Use the `on()` method to listen to client-side events like this:

```javascript
fera.on('submitter.show', (submitter) => console.log("Content submission modal was just shown."));
```

See [Javascript API Events](https://developers.fera.ai/js-api-events) for a list of events you can listen for.

# Other Methods

## Set the current language/locale

```
fera.setLocale('fr'); // Sets the storefront widget language to French. 
```

Source: https://developers.fera.ai/js-api-methods
