Skip to main content
Skip table of contents

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”.

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

  3. Give the Python script a Name.

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

  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.

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

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

Code snippet to used to create the article
PY
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.

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.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.