Skip to main content

Post Jobs with Credits

About 1 min

Post Jobs with Credits

Use your API key to create, extend, and stop jobs with Nosana credits. The SDK provides wrappers under Client.apiJobs.

  • balance() — check your available credits
  • list() — create a job using credits
  • extend() — add time to a running job
  • stop() — stop a running job

API referenceopen in new window.

Prerequisites

  • Install the SDK
npm install @nosana/sdk
  • An active API key

Get an API Key

You can create an API key from the Nosana dashboard.

  1. Log in at https://dashboard.nosana.com
  2. Go to Account
  3. Find the API Keys section
  4. Click Create Key
API keys overview
API keys overview
Create API key
Create API key

Initialize the client with API key

import { Client, sleep } from '@nosana/sdk';

const API_KEY = process.env.NOSANA_API_KEY ?? 'nos_xxx_your_api_key';

const client = new Client('mainnet', undefined, {
  apiKey: API_KEY,
});

Check credit balance

const balance = await client.apiJobs!.balance();
const available = balance.assignedCredits - balance.reservedCredits - balance.settledCredits;
console.log(`Balance available: $${available.toFixed(2)}`);

Create a job using credits

Upload your job definition to IPFS, then create the job with the returned IPFS hash.

const jobDefinition = {
  "version": "0.1",
  "type": "container",
  "meta": {
    "trigger": "cli"
  },
  "ops": [
    {
      "type": "container/run",
      "id": "hello-world",
      "args": {
        "cmd": "for i in `seq 1 30`; do echo $i; sleep 1; done",
        "image": "ubuntu"
      }
    }
  ]
}

const ipfsHash = await client.ipfs.pin(jobDefinition);

// Markets can be found on the dashboard explorer
const market = '7AtiXMSH6R1jjBxrcYjehCkkSF7zvYWte63gwEDBcGHq';

const listResp = await client.apiJobs!.list({
  ipfsHash,
  market,
  timeout: 1800, // max. 30 minutes
});

console.log('Created job:', listResp);

Extend a running job

const extendResp = await client.apiJobs!.extend({
  jobAddress: listResp.jobAddress,
  extensionSeconds: 1800, // +30 minutes
});

console.log('Extended job:', extendResp);

Stop a job

const stopResp = await client.apiJobs!.stop({ jobAddress: listResp.jobAddress });
console.log('Stopped job:', stopResp);

Full example

import { Client, sleep } from '@nosana/sdk';

(async () => {
  try {
    const API_KEY = process.env.NOSANA_API_KEY ?? 'nos_xxx_your_api_key';

    const client = new Client('mainnet', undefined, {
      apiKey: API_KEY,
    });

    const balance = await client.apiJobs!.balance();
    const available = balance.assignedCredits - balance.reservedCredits - balance.settledCredits;
    console.log(`Balance available: $${available.toFixed(2)}`);

    const jobDefinition = {
      "version": "0.1",
      "type": "container",
      "meta": {
        "trigger": "cli"
      },
      "ops": [
        {
          "type": "container/run",
          "id": "hello-world",
          "args": {
            "cmd": "for i in `seq 1 30`; do echo $i; sleep 1; done",
            "image": "ubuntu"
          }
        }
      ]
    }

    const ipfsHash = await client.ipfs.pin(jobDefinition);

    // Markets can be found on the dashboard explorer
    const market = '7AtiXMSH6R1jjBxrcYjehCkkSF7zvYWte63gwEDBcGHq';

    const listResp = await client.apiJobs!.list({
      ipfsHash,
      market
    });

    console.log('Created job:', listResp);

    await sleep(10);

    // Extend the job
    const extendResp = await client.apiJobs!.extend({
      jobAddress: listResp.jobAddress,
      extensionSeconds: 1800, // +30 minutes
    });
    console.log('Extended job:', extendResp);

    // Stop the job
    const stopResp = await client.apiJobs!.stop({ jobAddress: listResp.jobAddress });
    console.log('Stopped job:', stopResp);
  } catch (e) {
    console.error('Example failed:', e);
    process.exit(1);
  }
})();
Last update: