Each file is complete on its own. Copy the one you need and replace the placeholders. A comment above each says where to find the value.
Scripts#
-
get-token.sh
Asks for a token with curl and prints the answer.
-
get-token.mjs
Node: gets a token and keeps it in memory until shortly before it runs out.
-
get_token.py
Python: gets a token and keeps it in memory, with the standard library only.
-
send-lead.mjs
Node: gets a token, sends a lead with an Idempotency-Key, and checks what became of it.
-
token-server.mjs
Node: a small token endpoint for your pages, GET /subcontext-token.
-
get-token.php
PHP: gets a token and keeps it in a small private file until shortly before it runs out.
-
send-lead.php
PHP: gets a token, sends a lead with an Idempotency-Key, and checks what became of it.
-
token-server.php
PHP: a small token endpoint for your pages, for PHP's built-in web server or your own.
.NET#
One small C# (.NET 8) project with three modes: get-token, send-lead and
token-server. Save both files in a folder called dotnet. Copy the
SubcontextTokens class into your own code.
-
dotnet/Program.cs
C# (.NET 8) with three modes, get-token, send-lead and token-server. Copy the SubcontextTokens class into your own code.
-
dotnet/SubcontextExamples.csproj
The project file for the .NET 8 examples, with no NuGet packages.
Pages#
-
quote-form-with-your-token.html
The quote form with getToken calling your token endpoint.
-
call-the-api-from-the-browser.html
Your own JavaScript listing your products with a token from your token endpoint.
Running the scripts#
The scripts read their settings from environment variables, so the secret isn't stored in the file.
| Variable | Used by | What it is |
|---|---|---|
SUBCONTEXT_CLIENT_ID |
all | The application's Client ID |
SUBCONTEXT_CLIENT_SECRET |
all | The application's Client secret |
SUBCONTEXT_SCOPE |
the get-token examples | Optional. Scopes to ask for, default read. |
SUBCONTEXT_CAMPAIGN_KEY |
the send-lead examples | The campaign's key |
ALLOWED_ORIGIN |
the token servers | Optional. The website allowed to ask for tokens, default http://localhost:4100. |
PORT |
token-server.mjs, dotnet token-server |
Optional. Default 8081. (For token-server.php, the port is in the php -S command.) |
SUBCONTEXT_BASE_URL |
all | Optional. Default https://subcontext.com. |
Each script needs only its language's runtime:
- Node scripts: Node 18 or later.
- Python: Python 3.8 or later.
- PHP: PHP 8.1 or later, with no extensions beyond the standard ones.
- .NET: the .NET 8 SDK. No NuGet packages.
Run them in the folder you saved them to:
# A server-only application
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... sh get-token.sh
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... node get-token.mjs
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... python3 get_token.py
# A server-only application with read, write and crm. Sends a real lead, so
# put your own details in the LEAD near the top of the file first.
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... SUBCONTEXT_CAMPAIGN_KEY=... node send-lead.mjs
PHP and .NET, with the same settings:
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... php get-token.php
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... SUBCONTEXT_CAMPAIGN_KEY=... php send-lead.php
cd dotnet
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... dotnet run -- get-token
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... SUBCONTEXT_CAMPAIGN_KEY=... dotnet run -- send-lead
On Windows PowerShell, set each variable first ($env:SUBCONTEXT_CLIENT_ID = "...")
and then run the command on its own.
Trying the pages#
- Create a browser application with
readandwrite, andhttp://localhost:4100under Allowed origins. - Start the token server with its Client ID and Client secret, in the folder you saved it to:
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... node token-server.mjs
Or, in PHP or .NET:
SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... php -S localhost:8081 token-server.php
cd dotnet && SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... dotnet run -- token-server
Each answers on http://localhost:8081/subcontext-token.
- In each page, set
TOKEN_ENDPOINTtohttp://localhost:8081/subcontext-token. In quote-form-with-your-token.html, also set your product and journey slugs (on the product's and journey's pages in the dashboard, under Products). - Save the page and open it on a local web server, for example
python3 -m http.server 4100in the folder you saved it to, thenhttp://localhost:4100/<file name>. Opening the file straight from disk (file://) won't work, because the website check needs a real web address. - When you've finished, delete the test application, or at least remove
http://localhost:4100.