1. API Keys
  2. Write API Keys

API Keys

Write API Keys

Create API Key

POST
`/v1/api-keys`

API Key Generation and Management:

  1. Initial Creation:
    • Upon creation, the full API key is displayed only once in the api_key property of the response.
    • Option to provide a custom name for the API key.
  2. Scope Assignment:
    • Scopes can be defined using either integer values or string literals (read, write, and null).
      • Write Access: Can be represented as 2 or write
      • Read access: Can be represented as 1 or read
      • No access: Can be represented as 0 or null
    • Important: Scope assignment is restricted by the permissions of the current API key in use. You can only add scopes that are already present in your current key.
js
        const data = {
  name: "My First API Key",
  scopes: {
    items: 1 //read scopes
    shipments: 2 //write scopes,
    fulfillments: null //remove scopes
  }
  //Set an expiration date for the API key.
  //This is optional and default to null, which means the API key will never expire.
  //You can pass in ISO 8601 date string, a JS Date object, or a timestamp in epoch seconds or milliseconds.
  expires_at: "2024-03-25T14:30:00Z"
};

const res = await fetch(`https://api.packagex.io/v1/api-keys`, {
  method: "POST",
  headers: {
    "PX-API-KEY": process.env.PX_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(data),
}).then((res) => res.json());

const key = res.data.api_key;

      

Update API Key

POST
`/v1/api-keys/:api-key`

You can update the name and scopes of your API Key.

js
        const data = {
  name: "Updated name",
  scopes: {
    items: "write" //you can use string if you want
    shipments: "read"
    fulfillments: null
  }
};

await fetch(`https://api.packagex.io/v1/api-keys/${api_key}`, {
  method: "POST",
  headers: {
    "PX-API-KEY": process.env.PX_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify(data),
}).then((res) => res.json());