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.api:
client.api.credits.balance()— check your available creditsclient.api.jobs.list()— create a job using creditsclient.api.jobs.extend()— extend the maximum timeout of a jobclient.api.jobs.stop()— stop a job
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.
- Log in at 
https://dashboard.nosana.com - Go to 
Account - Find the 
API Keyssection - Click 
Create 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.api.credits.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.api.jobs.list({
  ipfsHash,
  market,
  timeout: 1800, // max. 30 minutes
});
console.log('Created job:', listResp);
Extend a running job
const extendResp = await client.api.jobs.extend({
  jobAddress: listResp.jobAddress,
  extensionSeconds: 1800, // +30 minutes
});
console.log('Extended job:', extendResp);
Stop a job
const stopResp = await client.api.jobs.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.api.credits.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.api.jobs.list({
      ipfsHash,
      market
    });
    console.log('Created job:', listResp);
    await sleep(10);
    // Extend the job
    const extendResp = await client.api.jobs.extend({
      jobAddress: listResp.jobAddress,
      extensionSeconds: 1800, // +30 minutes
    });
    console.log('Extended job:', extendResp);
    // Stop the job
    const stopResp = await client.api.jobs.stop({ jobAddress: listResp.jobAddress });
    console.log('Stopped job:', stopResp);
  } catch (e) {
    console.error('Example failed:', e);
    process.exit(1);
  }
})();