Saving data from a web page
You can insert and fetch data from Val Town from any web page using the Run API and some JavaScript.
1. Create a val to store some state
Fork, or copy and paste this val into your workspace – keep it named as comments
so the snippets in this guide work correctly.
2. Create a val that can mutate that state
Fork, or copy and paste the below val into your workspace.
When it’s set to public, anyone can send an API request to it, and the data they send will be appended to the comments
val you created in the previous step.
3. Use this JavaScript snippet on your webpage
Get the Run API endpoint for the val you created in the step above.
The below JavaScript snippet sends data to your appendToComments
val which writes the data to your comments
val.
4. Directly read from comments
If you make your comments
val public, anyone can read from it directly with the Run API with this JavaScript snippet.
When the Fetch API doesn’t include a method, it sends a GET request.
5. Indirectly read from comments
Maybe you want to hide some of your data. You can keep comments
private and just provide access to a slice of it by using a public function val like this.
Go ahead and test this by calling this one in your browser’s developer console with this snippet.
If the val is a function, it’s implicitly called by the Run API.
6. Use anything that supports HTTP!
Like cURL
:
Or python3
:
7. See the Val Town API reference
For more details, and alternative ways of interacting with Val Town, see Val Town API