Setup and configuration guides

How-to create an HTTP API webhook

Advanced

This article will guide how to create a webhook in RS Production. A webhook is a way to extend the RS Production HTTP API.

A webhook is a PythonScript object, all PythonScript objects are available as webhooks over the HTTP API. In the following guide, we will create a PythonScript object that can be called

The implemented webhook will create a new article using two parameters:

  • ArticleNumber : string

  • ArticleName : string

 Instructions

  1. Open RS Start and start to write “pythonscript” and then select the option “Python script”.

    image-20230118-155018.png


  2. Press “Create” to create a new Python script instance.

    image-20230118-155158.png


  3. Give the Python script a Name.

    image-20230118-155346.png


  4. Save the Changes using the “Save”-button and then go to the “Parameters” section to the left.

    image-20230118-155656.png


  5. Create two parameters of type string: ArticleName and ArticleNumber. The parameters will be available to set from the HTTP API and will be available to use from the python script.

    image-20230118-160139.png


  6. Go back to the “Information” section and click on the “Python Code” link in order to open the Python code editor.

    image-20230118-160401.png


  7. Time to write the actual code that creates a new article using the two parameters added previously.

    image-20230118-161319.png
Code snippet to used to create the article
Python
import System
from System import *
import clr
clr.ImportExtensions(System.Linq)
from DataRepositoryShared import *

# create a new article instance
article = _environment.Model.CreateNew(RSEntityTypes.RSArticle)

# use the parameters set the ArticleName and ArticleNumber properties on the new instance
article.Name = ArticleName
article.ArticleNumber = ArticleNumber

# save the new instance to the model
_environment.Model.Save(article)

Press save and close!

The PythonScript instance is finished and ready to be invoked over HTTP.

Invoke the Python script as a webhook over HTTP

More details about how to invoke a webhook can be found here: HTTP API / JSON API.

The WebhookId is the same id as the PythonScript instance, get the id by right-clicking the python script instance in the list and select “Copy selected GUID” from the context menu.

image-20230118-162251.png

Example code

HTML
POST /INSTALLATION_NUMBER/api/v1/webhook/ HTTP/1.1
Host: INSTALLATION_NUMBER.rsproduction.se
Authorization: Bearer YOUR_API_AUTHORIZATION_TOKEN
Content-Type: application/json
Content-Length: 133

{
    "webhookId": "WEBHOOK_ID",
    "ArticleName": "The new article",
    "ArticleNumber": "1001-01"
}

Where

  • INSTALLATION_NUMBER

    • The installation number of the installation in the RS Production cloud where you want to execute the webhook.

  • YOUR_API_AUTHORIZATION_TOKEN

    • Your authorization token in order to access the HTTP API.

  • WEBHOOK_ID

    • The id (GUID) of the python script instance.

Result

After posting the example code over HTTP to the RS Production API the following article is added.

image-20230118-164852.png