Creating your first script can seem challenging. We’ve created this tutorial to make it much easier. Follow the steps to create your first script and have it running in no time.
Create the script
When you first log into scriptr.io you’re brought to your workspace which displays a welcome message. The workspace is an IDE (Integrated Development Environment) consisting of an editor, console area and the ability to save and run the script.
If you’re a programmer and already well-versed in writing API scripts, click inside the editor area and begin coding. For everyone else, you’re now staring at a blank editor screen. This might be scary but we’ll walk you through it.
As mentioned, click inside the editor area to begin. Any place is fine; you’ll be brought to line 1 and can begin typing. For this tutorial we’ll start intentionally easy. We’re going to create a script which simply displays a hello message to the user.
2. On line 1 enter this code:
return 'Hello from Scriptr.io!'
That’s it, the whole script. Nice and easy. Feel free to copy and paste this into your editor.
Here’s what your workspace editor should look like:
Save and Name the script
Now that you’ve written the script, it’s time to name and save it.
- Click the area at the top which says Set Script Name. Enter the name “helloscriptr” and press Enter.
- Now click the Save button on the top of the workspace.
Your script should now have the new name:
And you’ll notice it’s listed under My Scripts:
Run the Script
Now it’s time to test your script. You can do this right from the scriptr.io workspace by clicking the Run button. The results are displayed in the bottom console area.
Successful output will contain:
"status": "success",
"statusCode": "200"
The output also contains the script results, in this case:
"result": "Hello from Scriptr.io!"
You’ve now written and succesfully run your first script!
Obtain your token
To use your new scriptr.io script in a production environment (any place outside of your scriptr.io workspace) you must include your authorization token. It’s automatically generated in the console output:
-H 'Authorization: bearer UEJCODJEQjZCODpzY3JpcHRyOjhFRTNGQkU5MUNBREY2MjQxNjcyMzczMUUwNzhDNUNF'
Include this complete string when issuing the script. You can see the example already in the console output:
curl -X POST -H 'Authorization: bearer UEJCODJEQjZCODpzY3JpcHRyOjhFRTNGQkU5MUNBREY2MjQxNjcyMzczMUUwNzhDNUNF' 'https://api.scriptrapps.io/helloscriptr'
Your token is tied to your account; including your token on scripts utilizes your own access credentials. Therefore when creating scripts for others to use you may wish to use an anonymous token. To do this, click the box labeled Allow Anonymous Requests.
Both the anonymous and regular tokens can be found in your account information.
Issue the script
Your new script is now a customized API endpoint, and can be issued from a number of environments. You can issue it from a terminal window using the curl command as shown in the previous example.
You can also issue API endpoints directly in your web browser’s address field.
Since a web browser handles authentication differently, you must replace -H 'Authorization: bearer'
with auth_token
to allow the script to run:
https://api.scriptrapps.io/helloscriptr?auth_token=UEJCODJEQjZCODpzY3JpcHRyOjhFRTNGQkU5MUNBREY2MjQxNjcyMzczMUUwNzhDNUNF
When using a script for your own use you may use your own token. In other cases, such as when your scripts are used by others, you should use an anonymous token.