# Val Town

### Val Town

[Val Town](https://www.val.town/) is a service for running lightweight Javascript/Typescript code blocks in the cloud.

### Running Web API Vals from PixieBrix

{% hint style="info" %}
📚 View [the Val Town Web API documentation](https://docs.val.town/api/web)
{% endhint %}

Val Town’s Web API lets you expose Vals as standard API endpoints.

#### Locate the Val Web API Endpoint

In Val Town, click the Val’s three-dot menu and copy the web endpoint:<br>

<figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2Fp5B302Kw8FyFVa12kXyy%2Fimage.png?alt=media&#x26;token=68b1c8d8-f468-4f67-b07a-ad17c5b8b1e9" alt=""><figcaption></figcaption></figure>

#### Call the Val Web API Endpoint from PixieBrix

In PixieBrix, add the HTTP Request and call the Val’s Web API endpoint as a standard API:

* URL: the Web API endpoint
* Method: *refer to the Val for allowed methods*
* Search Parameters: **refer to the Val for available search/query parameters**
* Headers: **refer to the Val for the required/optional headers**
* JSON Data: ***refer to the Val for the JSON payload***\ <br>

  <figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2FBX4drPTGItda7gkA0xX2%2Fimage.png?alt=media&#x26;token=341861a8-31c9-4df6-bf14-ee05134d41b9" alt=""><figcaption></figcaption></figure>

### Running Vals from PixieBrix

{% hint style="info" %}
📚 View [the Val Town Run API](https://docs.val.town/api/run) [documentation](https://docs.val.town/api)
{% endhint %}

Val Town’s Run API lets you run any Val as an API

#### Locate the Val Run API Endpoint

In Val Town, use the three-dot menu to Endpoints > Copy run endpoint<br>

<figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2FxXvPJHC7r6X9gdrAV8d1%2Fimage.png?alt=media&#x26;token=37aeb12e-347e-4120-aff8-c181df206c8f" alt=""><figcaption></figcaption></figure>

#### Construct the Args Payload

If your Val expected arguments, you must construct the arguments for the call. The Run Endpoint expects an **array** of arguments.

In PixieBrix, there are two bricks available for creating a JSON array payload:

* Parse JSON
* jq - JSON processor brick

For example, to construct a hard-coded payload with the jq brick:<br>

<figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2FfEKUNgjyxFOBsZ2nOCiW%2Fimage.png?alt=media&#x26;token=2646023d-6780-4a67-9d54-e77933f90aa4" alt=""><figcaption></figcaption></figure>

Constructing a hard-coded payload with the jq brick

#### Calling the Unlisted/Public Val

To make a request to the Run endpoint for a Val, add the HTTP Request. Configure the HTTP Request brick:

* URL: the URL for the Run endpoint for your val
* Method: post
* JSON Data: add an `args` property, and pass the array of arguments\ <br>

  <figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2FglartXEYZb5JEBpQ8WKz%2Fimage.png?alt=media&#x26;token=caf4a31c-3911-4384-a597-35fb000aaba2" alt=""><figcaption></figcaption></figure>

Passing arguments to the Val Run API

### Making Authenticated Requests

{% hint style="info" %}
📚 For general information on configuring integrations, see [Configuring Integrations](https://www.notion.so/Configuring-Integrations-519952f1528d4080acc9504039bdd1d8?pvs=21)
{% endhint %}

To make a request to a private Val, you must pass your Val Town API Token with the request.

#### Configuring a Val Town Integration in PixieBrix

{% hint style="info" %}
To set up a cloud integration that your team can access, head to the [Admin Console](https://app.pixiebrix.com/). *(If you'd rather set up something locally, use the Extension Console. Learn more about* [configuring-integrations](https://docs.pixiebrix.com/integrations/configuring-integrations "mention").*)*
{% endhint %}

From the **Integrations** section in the Admin or Extension Console, click the **+** **Add Integration** button in the top right corne&#x72;**.**&#x20;

Search for and select the Val Town integration.

Configure the Val Town integration, providing your Val Town API Token from <https://docs.val.town/api/authentication><br>

<figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2FuhIPpcpLnQzkgq3G909g%2Fimage.png?alt=media&#x26;token=e34fe475-3ace-45bc-9af9-93babcb52e6b" alt=""><figcaption></figcaption></figure>

#### Locate the Val Run API Endpoint

In Val Town, copy the run fetch or express fetch endpoint for the val<br>

<figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2Fbxfl5Z0KRKWR1qIg4s8E%2Fimage.png?alt=media&#x26;token=14383caf-9b68-4482-913c-af42d8e9f1b0" alt=""><figcaption></figcaption></figure>

From the copied endpoint, note the:

* URL
* Method

```jsx
fetch("<https://api.val.town/v1/run/twschiller.emailMe>", {
  method: "GET",
  headers: { Authorization: "REDACTED" }, // DO NOT PUBLISH THIS AUTHENTICATION KEY
});
```

#### Call the Private Val

In the PixieBrix Page Editor, add the HTTP Request brick, and provide the following information:

* URL: the URL for the Val
* Integration Configuration: the integration you configured
* Method: the HTTP method copied from the endpoint

<figure><img src="https://2274778196-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fq123bF1HPQPV35s5vHa1%2Fuploads%2FwmL2tJ7l2weozQgyKf57%2Fimage.png?alt=media&#x26;token=41135a4b-9741-4a03-82be-1ef5f95f43cb" alt=""><figcaption></figcaption></figure>
