The goal of this tutorial is to build a simple ByteNite app that outputs the string "Hello, World!" to a file. Use this tutorial if you're new to ByteNite and want to give it a spin.
Duration
Difficulty
Prerequisites
~15m
Easy
1
Download sample app
Run the app new command in your terminal to create an app directory locally named "hello-world":
bytenite app new hello-world
Check that a new directory named "hello-world" was indeed created at your base path:
ls
2
Write the run function
Locate your pre-generated entrypoint (./hello-world/app/main.py) and add the hello world code to your run function:
main.py
def run(basepath, params, metadata):
print('Run called')
# Write your code here
output_string = "Hello, World!"
output_path = os.path.join(basepath, OUTPUT_PATH, "hello_world.txt")
with open(output_path, 'w') as f:
f.write(output_string)
3
Submit and activate your app
Upload the content of your app:
bytenite app upload hello-world
Activate your app to make it accept jobs:
bytenite app activate hello-world
Check for an "Activate successful" log message, or manually check the status of your app to ensure it's active:
Once your app is uploaded and activated, you can run it through the Jobs API.
Send a POST request to the endpoint to create a new job, and then run it. All requests must be authenticated with an access token.
Tip
If you previously authenticated through the CLI, you don't need an API key to generate a token as you already have a fresh one stored in your application library:
Token path on Mac: /Users/[user]/Library/Application Support/bytenite-cli/auth.json
Token path on Linux: /$HOME/.config/bytenite-cli/auth.json
import requests
import json
# Extract your token from CLI credentials. Alternatively, get a token with an API key.
token_path = "/Users/[user]/Library/Application Support/bytenite-cli/auth.json"
with open(token_path, 'r') as file:
data = json.load(file)
access_token = data["access_token"]
# Create a new job with '[email protected]'
response = requests.post('https://api.bytenite.com/v1/customer/jobs',
json={
"name": "My job",
"templateId": "[email protected]"},
headers={'Authorization': access_token})
job_id = response.json()['job']['id']
print(f'Job ID: {job_id}')
# Run the job
response = requests.post(f'https://api.bytenite.com/v1/customer/jobs/run/{job_id}',
headers={'Authorization': access_token})
response.raise_for_status()
5
Check your results
Wait a few seconds and check out the results. Hello, ByteNite!