The example files

Complete examples in curl, Node, Python, PHP and C# for getting a token, sending a lead and running a token endpoint, plus two pages to try in a browser.

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#

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

Pages#

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:

sh
# 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:

sh
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#

  1. Create a browser application with read and write, and http://localhost:4100 under Allowed origins.
  2. Start the token server with its Client ID and Client secret, in the folder you saved it to:
sh
   SUBCONTEXT_CLIENT_ID=... SUBCONTEXT_CLIENT_SECRET=... node token-server.mjs

Or, in PHP or .NET:

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

  1. In each page, set TOKEN_ENDPOINT to http://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).
  2. Save the page and open it on a local web server, for example python3 -m http.server 4100 in the folder you saved it to, then http://localhost:4100/<file name>. Opening the file straight from disk (file://) won't work, because the website check needs a real web address.
  3. When you've finished, delete the test application, or at least remove http://localhost:4100.

Stuck, or something missing?

Get in touch and a person on the team will help.

Contact us