All the code in this repository is public. Developers copy it into their own projects. These rules make the examples consistent and safe to copy.
To make a new example, copy the examples/_template/ directory.
- Put each example in its own directory in
examples/. - Each example must run after a new clone of the repository. Do not add steps that are not in the README of the example.
- The
nameinpackage.jsonmust be the same as the directory name. - If the example has a
package.json, give the minimum Node version inengines. The minimum version for the repository is in.nvmrc. - If the example has no
package.json, no tool can check the minimum version. Do not put flags in the README that need a specific version of Node. Use environment variables.
- Read the API key from an environment variable.
- Do not put a key in the code. Do not print a key. Do not commit a key.
- Add a
.env.examplefile. Leave each value empty. Put a comment above each variable with the URL where the user gets it. A value that is not empty passes the check for a missing key. Then the example stops with an API error, not with your message. - Tell the user in the README how to load the file. Use
exportcommands. If the example is only in Node.js and has apackage.json, you can usenode --env-file=.envinstead. - For a Next.js example, name the file
.env.local.example. Next.js reads.env.local. - Add the real environment file to
.gitignore.
- The
.prettierrcfile in the root directory controls the format of the JavaScript, TypeScript, JSON, CSS and Markdown files. - The
ruff.tomlfile controls the format of the Python files. - If the example has a
package.json, add aformatscript. - The CI pipeline does
prettier --checkandruff checkfor each pull request. Do these commands on your computer first.
An example can fail for these usual reasons:
- The user did not set the API key.
- The API rejected the key.
- The network failed.
For each of these failures, the example must print one line that tells the user what to do. Then the example must stop with an exit code that is not zero. Do not let the example print a stack trace.
Most users get the first failure, because they forgot to set the API key. Give the most attention to that failure.
In Python, catch Exception. A list of exception classes is easy to get wrong. KeyboardInterrupt is not
an Exception. Catch it also if the example waits in a loop.
Use these sections, in this sequence, in each README:
- What the example does, and a link to the related guide if there is one
- Requirements
- Setup
- How to run the example
- What happens when you run it
Write as few statements of fact as possible. Each version number, flag and command in a README is a statement that must stay correct.
Write the documentation in Simplified Technical English (ASD-STE100). Many readers of this repository do not have English as their first language. Obey these rules:
- Write short sentences. Use a maximum of 20 words in an instruction, and 25 words in a description.
- Write one instruction in each sentence.
- Use the active voice. Write "Set the API key", not "The API key must be set".
- Use the same word for the same thing each time.
- Do not use idioms, metaphors or humour.
You must read the code and run it before you make a pull request. Then do these checks:
-
Clone the repository again. Run the example with no API key. Read the message.
-
Run the example with an incorrect API key. Read the message.
-
Do each command in your README, in the correct sequence.
-
Run the formatter and the linter.
-
If the example is in two languages, run both with these inputs:
- no API key
- an incorrect API key
- an input file with a malformed row
The messages must be the same. If they are different, one of the files is incorrect.